Reputation: 6606
Lately I found a bug in a huge system I'm working on caused by this behaviour:
Consider this:
define('TEST',10);
echo TEST; // prints 10
define('TEST',20); // Error -> already assigned.
But if we declare it as insensitive:
define('TEST',10,true);
echo TEST; // prints 10
define('TEST',20); // no error ????
echo TEST; //prints 20
I understand what are the differences between CS and CI and I realise that I'm creating a new constant in the second definition. But I really don't understand why is that possible?
Isn't that a violation of the constant concept? Does this behaviour has any applications or is it a PHP weird thing...
Upvotes: 1
Views: 1514
Reputation: 59691
Because your first constant (which you saved as case-insensitive) is saved in lowercase as you can read it in the manual:
Note: Case-insensitive constants are stored as lower-case.
Means since it is case-insensitive all variants of lower and upper case from test
, which are != TEST
in uppercase are corresponding to the value 10
. If it is TEST
which is case-sensitive means every letter in uppercase it is the constant with the value 20
.
E.g.
Test -> 10
tEst -> 10
tesT -> 10
TEST -> 20
And a "special case" is also TEST
if you use it before you define your case-sensitive constant it is still pointing to the constant with the value 10
.
Upvotes: 4
Reputation: 31749
When you are doing define('TEST',10,true);
it is stored in lower case
but you can access them by both test
& TEST
.
Now there is no constant
with the name of TEST
. So when your defining it again the value is set to the constant
.
Upvotes: 0