gpt4 book ai didi

php - 我可以使用 define 方法更改常量值。如何克服这个问题...?

转载 作者:行者123 更新时间:2023-12-04 02:24:32 25 4
gpt4 key购买 nike

如果我在 DEFINE 方法中将可选的第三个参数作为 TRUE 传递,它允许我覆盖常量的现有值。这不是预期的行为,因为常量的值一旦声明就不应更改。

测试脚本:

define("GREETING", "Hello you.",true); 
echo "before changing the constant value ";
echo GREETING; // outputs "Hello you."

define("GREETING", "sample value.");
echo GREETING; // outputs "sample value."

预期结果:

constant's value should not be changed once it is declared.

实际结果:

it is allowing me to override the existing value of a constant.

最佳答案

实际上,您并没有重新声明 CONSTANT,而是当您使用第三个 boolean true 声明一个不区分大小写的常量时,您声明的常量将在幕后小写,例如:

define("GREETING", "Hello you.",true);

在内部它是用 lowercase 定义的,当你再次定义时:

define("GREETING", "Hello you.");

它现在以 UPPERCASE(默认)定义,因此您有两个不同的常量,它们是:

greeting // first one because of "true" but we don't see it
GREETING // second one

作为证明,您不能使用 true 重新声明相同的常量两次,例如:

define("GREETING", "Hello you.",true); 
echo "before changing the constant value <br />";
echo GREETING;
define("GREETING", "sample value.", true);
echo GREETING;

输出(相同):

before changing the constant value 
Hello you.Hello you.

关于 case_insensitive 参数:

If set to TRUE, the constant will be defined case-insensitive. The default behavior is case-sensitive; i.e. CONSTANT and Constant represent different values.

因此,CONSTANT 是不可更改的,它始终是 CONSTANT,正如其名称所示。阅读手册 ( User Contributed Notes )。

关于php - 我可以使用 define 方法更改常量值。如何克服这个问题...?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23988305/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com