比较$foo is_array或$foo是false


Comparing $foo is_array or $foo is false

我正在尝试比较以下内容:如果值是而不是数组,或者值不等于false,则返回

if (is_array($value) != true || $value != false) return;

这个,以及我尝试的任何其他变体似乎都不起作用。然而,当我在它们自己的if语句中单独比较它们时,它们会返回正确的结果。

如有任何帮助,我们将不胜感激!

您最初说"如果它不是数组或不是false"。你在倒过来想。您想要"如果值是数组或false继续,否则返回"。

因此:

// This says: "if it's not (an array or false)"
if(!(is_array($value) || $value === FALSE)) return;

使用德摩根定律,我们可以将其转换为

// This says: "if it's not an array and not false"
if(!is_array($value) && $value !== FALSE) return;

我会这样写:

if (!is_array($value) || $value !== false)

php中的许多内容都可以是===false,但只有布尔值false可以是===true。查看布尔型手动页面和比较运算符手动页面