Kathir
Kathir

Reputation: 39

php constant case-insensitive issue

I'm overriding a constant with case in-sensitive parameter. But php doesn't give me "constant already defined" error. I've enabled E_ALL,E_STRICT errors. Example:1

define('ONE',1000);
define('one',2000,true);
echo ONE; // prints 1000
echo one; // prints 2000

In the second line, i'm making 'one' as another constant with case in-sensitive, which means redefining 'ONE'. But PHP gives no error/warning.

Example:2

define('ONE',1000,true);
define('one',2000);
echo ONE; // prints 1000 with constant already defined notice
echo one; // prints 1000

Here i can get error notice.

What's the difference between these two code blocks.?

Upvotes: 3

Views: 2354

Answers (2)

Niko
Niko

Reputation: 26730

From the documentation:

Note: Case-insensitive constants are stored as lower-case.

Thus, when trying to define the lower-cased version of the constant in your second example, the constant is already defined due to the prior case-insensensitive definition of a constant with the same name.

define('ONE', 1000, true);  // defines strtolower("ONE") = "one"
define('one', 2000);        // error redefining "one"

In the first scenario, there is no such collision:

define('ONE', 1000);        // defines "ONE"
define('one', 2000, true);  // defines strtolower("one") = "one"

Upvotes: 6

c4pone
c4pone

Reputation: 797

The third Parameter in the define function is the case_insensitive option. http://php.net/manual/de/function.define.php

In the first Example the constant ONE ist defined. And the constant one with case_insensitive true. Means you got a variable you can reach via ONE and a variable you can reach via oNe,One,oNE etc.

In the secound Example you first define a constant ONE with case_insensitive true and then the constant one. But this time all possible names (OnE,oNe,one) are already given, so the interpreter gives you a error notice

Upvotes: 0

Related Questions