简体   繁体   中英

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

I can't understand why this code is executed is not the way I want.

define('TEST', 123);
echo TEST;
echo "\n";
var_dump( defined(TEST) );

print:

123
bool(false)

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!

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') );

可能是因为 defined() 需要一个字符串作为参数。

var_dump( defined('TEST') );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM