技能训练企业网站建设可行性分析企业网站建设 百度文库
news/
2025/10/8 22:50:26/
文章来源:
技能训练企业网站建设可行性分析,企业网站建设 百度文库,南充二手房最新出售信息,网站建设推荐公司文章目录 参考环境常量数组不可变性版本限制 constdefine()构造大小写不敏感的常量$case_insensitive 参数PHP7.3PHP8 若 define() 在不支持常量数组的版本中运行 参考
项目描述搜索引擎Bing、GoogleAI 大模型文心一言、通义千问、讯飞星火认知大模型、ChatGPTPHP 手册PHP Man… 文章目录 参考环境常量数组不可变性版本限制 constdefine()构造大小写不敏感的常量$case_insensitive 参数PHP7.3PHP8 若 define() 在不支持常量数组的版本中运行 参考
项目描述搜索引擎Bing、GoogleAI 大模型文心一言、通义千问、讯飞星火认知大模型、ChatGPTPHP 手册PHP Manual
环境
项目描述PHP5.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 Valuesint、 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 4const
在 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 Valuesint、 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
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/932025.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!