文章目录
- 参考
- 环境
- 常量数组
- 不可变性
- 版本限制
 
 
- const
- define()
- 构造
- 大小写不敏感的常量
- $case_insensitive 参数
- PHP7.3
- PHP8
 
 
 
 
- 若 define() 在不支持常量数组的版本中运行
参考
| 项目 | 描述 | 
|---|---|
| 搜索引擎 | Bing、Google | 
| AI 大模型 | 文心一言、通义千问、讯飞星火认知大模型、ChatGPT | 
| PHP 手册 | PHP Manual | 
环境
| 项目 | 描述 | 
|---|---|
| PHP | 5.5.0、5.6.8、7.0.0、7.2.5、7.4.9、8.0.0、8.2.9 | 
常量数组
不可变性
在PHP中,常量数组是指 包含在一个常量中的数组。与普通变量不同,常量一旦被定义,就不能被修改或重新赋值。常量数组允许你在代码中定义一个不可更改的数组,这在某些情况下可能非常有用。对此,请参考如下示例:
<?phpconst ARR = [1, 2, 3];# 尝试修改常量数组将导致异常的发生
ARR[0] = 999;var_dump(ARR);
执行效果
上述示例中,我们尝试修改常量数组中的内容,这导致 PHP 抛出 Fatal error 异常,PHP 程序随之终止。
Fatal error: Cannot use temporary expression in write context in C:\test.php on line 7
PHP Fatal error:  Cannot use temporary expression in write context in C:\test.php on line 7
版本限制
PHP 在 5.6 开始支持常量数组,此版本以前的 PHP 仅支持 Scalar Values(int、 float、string、boolean 或 null) 作为常量值,若将数组作为常量的值,则 PHP 将抛出 Fatal error 异常。对此,请参考如下示例:
<?phpconst ARR = [12, 3, 4];var_dump(ARR);
执行效果
尝试在 PHP5.5.0 版本中执行上述代码将产生如下结果:
Fatal error: Arrays are not allowed as constants in C:\test.php on line 4
PHP Fatal error:  Arrays are not allowed as constants in C:\test.php on line 4
const
在 PHP 中,const 是用于定义类常量的 关键字。自 PHP5.6 开始,const 开始支持定义值为数组的常量,即数组常量。
define()
构造
与 const 的功能类似,define() 也是 PHP 中用于定义常量的函数。define() 函数 在 PHP7.0.0 版本开始支持定义值为数组的常量。
define(string $constant_name, mixed $value, bool $case_insensitive = false): bool
其中:
| 项目 | 描述 | 
|---|---|
| $constant_name | 该参数的值需为一个 字符串,用于指示常量的名称。 | 
| $valuse | 用于指定常量的值,自 PHP7.0.0开始,该参数在支持Scalar Values(int、 float、string、boolean 或 null)的基础上,开始支持数组作为此参数的值。 | 
| $case_insensitive | 默认值为 false,若该值为true,则该函数将创建对变量名大小写不敏感的常量。 | 
大小写不敏感的常量
同 const 相比,define() 比较特殊的是该函数能够创建 大小写不敏感 的常量。对此,请参考如下示例:
<?phpdefine('ARR', array('One' => 'Hello', 'Two' => ' ', 'Three' => 'World', ), true);
define('arr', array(1, 2, 3));var_dump(ARR);
var_dump(ARR == arr);
var_dump(ARr == aRr);
执行效果
由于将 $case_insensitive 参数的值设置为 true,故创建了一个对变量名称大小写不敏感的常量数组。也正因此,名为 ARR、arr、ARr 及 aRr 的几个数组常量被认为是相等的(PHP 中,数组的比较不是引用的比较)。
PHP Notice:  Constant arr already defined in C:\test.php on line 5Notice: Constant arr already defined in C:\test.php on line 5
array(3) {["One"]=>string(5) "Hello"["Two"]=>string(1) " "["Three"]=>string(5) "World"
}
bool(true)
bool(true)
$case_insensitive 参数
PHP7.3
在 PHP7.3 版本中,define() 函数 中的 $case_insensitive 参数不再被支持设置为 true。您在这些版本中使用 $case_insensitive = true 仍然能够创建对大小写不敏感的常量,只是 PHP 将为此抛出 Deprecated 异常,但程序并不为此立即停止。对此,请参考如下示例:
<?phpdefine('ARR', array('One' => 'Hello', 'Two' => ' ', 'Three' => 'World', ), true);var_dump(ARR);
执行效果
在 PHP7.4.9 中执行上述代码将得到如下结果:
PHP Deprecated:  define(): Declaration of case-insensitive constants is deprecated in C:\test.php on line 4Deprecated: define(): Declaration of case-insensitive constants is deprecated in C:\test.php on line 4
array(3) {["One"]=>string(5) "Hello"["Two"]=>string(1) " "["Three"]=>string(5) "World"
}
PHP8
在 PHP8 中,$case_insensitive 参数被 正式废除,为了 向前兼容,您仍然能够使用 $case_sensitive = true,只是该值将被忽略,false 为该参数的 实际参数值,且 PHP 将为此抛出 Warning 异常。对此,请参考如下示例:
<?phpdefine('ARR', array('One' => 'Hello', 'Two' => ' ', 'Three' => 'World', ), true);var_dump(ARR);
var_dump(arr);
执行效果
尝试在 PHP8.0.0 版本中执行上述代码,得到结果如下:
PHP Warning:  define(): Argument #3 ($case_insensitive) is ignored since declaration of case-insensitive constants is no longer supported in C:\test.php on line 4
PHP Fatal error:  Uncaught Error: Undefined constant "arr" in C:\test.php:7
Stack trace:
#0 {main}thrown in C:\test.php on line 7Warning: define(): Argument #3 ($case_insensitive) is ignored since declaration of case-insensitive constants is no longer supported in C:\test.php on line 4
array(3) {["One"]=>string(5) "Hello"["Two"]=>string(1) " "["Three"]=>string(5) "World"
}Fatal error: Uncaught Error: Undefined constant "arr" in C:\test.php:7
Stack trace:
#0 {main}thrown in C:\test.php on line 7
由于使用了 $case_sensitive = true ,导致 PHP 产生 Warning 异常。我们假定 $case_sensitive = true 会起作用,但 PHP 为此产生了 Fatal error 异常,导致程序立即终止。
 上述示例代码若在 PHP7.3 及 PHP8(不含此版本) 之间的 PHP 版本中运行,将仅产生 Warning 异常,程序将继续正常执行。
若 define() 在不支持常量数组的版本中运行
若 define() 函数在 不支持常量数组的 PHP 版本 中运行,在你使用常量数组时,PHP 将 否认数组为该常量的值并假设常量名的字符串形式为该常量的值。对此,请参考如下示例:
<?phpdefine('I_AM_A_CONSTANT', [1, 2, 3, 4]);var_dump(I_AM_A_CONSTANT);
执行效果
Warning: Constants may only evaluate to scalar values in C:\test.php on line 4Notice: Use of undefined constant I_AM_A_CONSTANT - assumed 'I_AM_A_CONSTANT' in C:\test.php on line 6
string(15) "I_AM_A_CONSTANT"
PHP Warning:  Constants may only evaluate to scalar values in C:\test.php on line 4
PHP Notice:  Use of undefined constant I_AM_A_CONSTANT - assumed 'I_AM_A_CONSTANT' in C:\test.php on line 6