请解释这个php表达式"$变量“;


Please explain this php expression "!$variable"

变量前面的感叹号是什么意思?它是如何在这段代码中使用的?

编辑:从目前的答案来看,我怀疑我还应该提到,这段代码位于一个函数中,其中一个参数是$mytype。。。。这是检查$mytype是否通过的一种方法吗?-感谢迄今为止所有的响应者。

 $myclass = null;
    if ($mytype == null && ($PAGE->pagetype <> 'site-index' && $PAGE->pagetype <>'admin-index')) {
        return $myclass;
    }
    elseif ($mytype == null && ($PAGE->pagetype == 'site-index' || $PAGE->pagetype =='admin-index')) {
        $myclass = ' active_tree_node';
        return $myclass;
    }
    elseif (!$mytype == null && ($PAGE->pagetype == 'site-index' || $PAGE->pagetype =='admin-index')) {
        return $myclass;
    }`

PHP中的感叹号表示not:

if($var)

表示在时$var不为零或为假

if(!$var)

意味着$var是否为空或为零或为假。

可以将其视为一个类似以下内容的查询:

select someColumn where id = 3

select someColumn where id != 3

!否定它前面的任何值。因此,您发布的代码检查$mytype的否定值是否为==null

return true; //true
return !true; //false
return false; //false
return !false; //true
return (4 > 10); //false
return !(4 < 10); //true
return true == false; //false
return !true == false; //true
return true XOR true; //false
return !true XOR true; //true
return !true XOR true; //false
变量否定其值之前的!

该语句实际上与!$mytype相同,因为FALSE == NULL

!$mytype == null

如果$mytype包含以下内容之一,则语句将返回TRUE

  • TRUE
  • 零以外的数字
  • 非空字符串
elseif (!$mytype == null && ($PAGE->pagetype == 'site-index' || $PAGE->pagetype =='admin-index')) {
    return $myclass;
}

上述!$mytype == null是错误的。!$mytype表示,如果变量的计算结果为false或为null,则条件将执行。

然而,额外的== null是不必要的,并且基本上是说if (false == null)if (null == null)

!$variableIf条件一起用于检查变量是否为Nul l或非

例如

if(!$var)

意味着如果$var为空。