Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php constant case-insensitive issue

Tags:

php

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.?

like image 835
Kathir Avatar asked Mar 30 '26 17:03

Kathir


1 Answers

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"
like image 90
Niko Avatar answered Apr 02 '26 05:04

Niko