我正在用不区分大小写的参数覆盖常量。但是 php 没有给我“常量已经定义”的错误。我启用了 E_ALL,E_STRICT 错误。 示例:1
define('ONE',1000);
define('one',2000,true);
echo ONE; // prints 1000
echo one; // prints 2000
在第二行中,我将“one”作为另一个不区分大小写的常量,这意味着重新定义“ONE”。但是 PHP 没有给出错误/警告。
示例:2
define('ONE',1000,true);
define('one',2000);
echo ONE; // prints 1000 with constant already defined notice
echo one; // prints 1000
在这里我可以得到错误通知。
这两个代码块有什么区别?
最佳答案
来自documentation :
Note: Case-insensitive constants are stored as lower-case.
因此,当尝试在第二个示例中定义常量的小写版本时,由于先前对同名常量的不区分大小写的定义,常量已经定义。
define('ONE', 1000, true); // defines strtolower("ONE") = "one"
define('one', 2000); // error redefining "one"
在第一种情况下,没有这样的碰撞:
define('ONE', 1000); // defines "ONE"
define('one', 2000, true); // defines strtolower("one") = "one"
关于php常量不区分大小写的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15095399/