如果是常量,但没有定义?

Cam*_*eon 5 php constants isset defined

如果我将常数设为=''; 如何检查是否有内部的东西?

defined(),并不像我希望的isset()那样工作,因为它被定义 不适用于常量

有什么简单的方法吗?

Lin*_*een 12

手册说,那isset()返回是否"[...]变量设定,并没有NULL".

常量不是变量,因此您无法检查它们.不过你可以试试这个:

define('FOO', 1);

if (defined('FOO') && 1 == FOO) {
// ....
}
Run Code Online (Sandbox Code Playgroud)

因此,当您的常量被定义为空字符串时,您首先必须检查它是否实际defined,然后检查其值('' == MY_CONSTANT).


My1*_*My1 6

为了检查内部是否有东西,您可以使用(自 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)