PHP defined() why is it returns false, even if the constant is defined?

use have call it wrong

define('TEST', 123);
echo TEST;
echo "\n";
var_dump( defined(TEST) );//provide The constant name you are providing 123 so it not defined
//correct call would be
var_dump( defined('TEST') );

Probably because defined() require a string as parameter.

var_dump( defined('TEST') );

Because you're not referring to the constant named TEST - you're referring to whatever TEST contains.

Wrapped out, this is what you're doing (the code is right - there is no 123 constant):

define('TEST', 123);

var_dump( defined(TEST) ); // turns into the below statement
var_dump( defined(123) ); // false - no 123 constant

Refer to the constant name instead (enclose it in quotes):

define('TEST', 123);

var_dump( defined('TEST') ); // true, the TEST constant is indeed defined
//                ^    ^ Quotation marks are important!

Tags:

Php