Lin*_*een 12
该手册说,那isset()返回是否"[...]变量设定,并没有NULL".
常量不是变量,因此您无法检查它们.不过你可以试试这个:
define('FOO', 1);
if (defined('FOO') && 1 == FOO) {
// ....
}
Run Code Online (Sandbox Code Playgroud)
因此,当您的常量被定义为空字符串时,您首先必须检查它是否实际defined,然后检查其值('' == MY_CONSTANT).
为了检查内部是否有东西,您可以使用(自 PHP 5.5 起)空函数。为避免错误,我还会检查它是否存在。
if(defined('FOO')&&!empty(FOO)) {
//we have something in here.
}
Run Code Online (Sandbox Code Playgroud)
因为 empty 也将最false类似的表达式(如“0”、0 和其他内容,请参阅http://php.net/manual/de/function.empty.php了解更多)为“空”
你可以试试:
if(defined('FOO') && FOO ) {
//we have something in here.
}
Run Code Online (Sandbox Code Playgroud)
这应该适用于更多版本(可能在任何可以运行 yoda 条件的地方)
要进行更严格的检查,您可以执行以下操作:
if(defined('FOO') && FOO !== '') {
//we have something in here.
}
Run Code Online (Sandbox Code Playgroud)