<?php
declare(strict_types=1);
define('GREETING', '欢迎学习 PHP 常量');
const SITE_URL = 'https://example.org/php';
define('FRUITS', ['Apple', 'Banana', 'Orange']);
const COLORS = ['Red', 'Green', 'Blue'];
function readGlobalConstant(): string
{
return GREETING;
}
$cases = [];
// Case 1: define() case sensitivity & PHP 8.0+ removal of case-insensitive 3rd arg
$warnings = [];
set_error_handler(static function (int $severity, string $message) use (&$warnings): bool {
$warnings[] = [$severity, $message];
return true;
});
/** @noinspection PhpDeprecatedFunctionInspection */
define('LEGACY_CI', 'case-sensitive-in-php8', true);
$redefineOk = define('GREETING', 'cannot-change');
restore_error_handler();
$undefThrown = false;
try {
constant('greeting');
} catch (Error) {
$undefThrown = true;
}
$c1Actual = [
'greeting' => GREETING,
'undef_lowercase_throws_error' => $undefThrown,
'legacy_ci_upper_defined' => defined('LEGACY_CI'),
'legacy_ci_lower_defined' => defined('legacy_ci'),
'redefine_returned' => $redefineOk,
'warnings_captured' => count($warnings) === 2 && $warnings[0][0] === E_WARNING,
];
$c1Expected = [
'greeting' => '欢迎学习 PHP 常量',
'undef_lowercase_throws_error' => true,
'legacy_ci_upper_defined' => true,
'legacy_ci_lower_defined' => false,
'redefine_returned' => false,
'warnings_captured' => true,
];
$cases[] = [
'name' => 'define_and_case_sensitivity_php8',
'passed' => $c1Actual === $c1Expected,
'expected' => $c1Expected,
'actual' => $c1Actual,
];
// Case 2: Constants have global scope automatically inside functions
$c2Actual = readGlobalConstant();
$c2Expected = '欢迎学习 PHP 常量';
$cases[] = [
'name' => 'constant_global_scope',
'passed' => $c2Actual === $c2Expected,
'expected' => $c2Expected,
'actual' => $c2Actual,
];
// Case 3: const keyword and immutability
$c3Actual = [
'site_url' => SITE_URL,
'still_original_greeting' => GREETING,
];
$c3Expected = [
'site_url' => 'https://example.org/php',
'still_original_greeting' => '欢迎学习 PHP 常量',
];
$cases[] = [
'name' => 'const_keyword_and_immutability',
'passed' => $c3Actual === $c3Expected,
'expected' => $c3Expected,
'actual' => $c3Actual,
];
// Case 4: Predefined constants (PHP_VERSION, PHP_OS, PHP_INT_MAX, E_* bitmasks)
$c4Actual = [
'version_starts_84' => str_starts_with(PHP_VERSION, '8.4.'),
'os_non_empty' => PHP_OS !== '',
'int_max_64bit' => PHP_INT_MAX,
'e_error' => E_ERROR,
'e_warning' => E_WARNING,
'e_parse' => E_PARSE,
];
$c4Expected = [
'version_starts_84' => true,
'os_non_empty' => true,
'int_max_64bit' => 9223372036854775807,
'e_error' => 1,
'e_warning' => 2,
'e_parse' => 4,
];
$cases[] = [
'name' => 'predefined_constants_check',
'passed' => $c4Actual === $c4Expected,
'expected' => $c4Expected,
'actual' => $c4Actual,
];
// Case 5: Constant arrays via define() and const (PHP 7+)
$c5Actual = [
'first_fruit' => FRUITS[0],
'second_color' => COLORS[1],
'fruits_count' => count(FRUITS),
'colors_count' => count(COLORS),
];
$c5Expected = [
'first_fruit' => 'Apple',
'second_color' => 'Green',
'fruits_count' => 3,
'colors_count' => 3,
];
$cases[] = [
'name' => 'constant_arrays_define_and_const',
'passed' => $c5Actual === $c5Expected,
'expected' => $c5Expected,
'actual' => $c5Actual,
];
echo json_encode(['version' => PHP_VERSION, 'cases' => $cases], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . "\n";
本例需在 Windows x64、PHP 8.4 本地 CLI 环境。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、PHP 8.4 本地 CLI 环境。
入口文件:projects/php-constants-lab/explore-constants.php
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-constants.php,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、PHP 8.4 本地 CLI 环境。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联95章) · 含当前章节或实例 · mbskycom202612a1232.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1234.zip