2012-09-08 36 views
1

变量前面的exclamaton标记是什么意思?它在这段代码中如何使用?

编辑:从迄今为止的答案我怀疑我也应该提到,这段代码是在一个函数中,其中一个参数是$ 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; 
    }` 

回答

2

!变量阻却之前,它的价值

这种说法其实是一样的!$mytype因为FALSE == NULL

!$mytype == null 

语句将返回TRUE如果$mytype包含下列操作之一:

  • TRUE
  • 除零之外的数字
  • 非空字符串
4

PHP中的感叹号表示not

if($var) 

意味着如果$var不为空或零或假,而

if(!$var) 

意味着如果$var为空或零或假。

把它看成是沿着线查询:

select someColumn where id = 3 

select someColumn where id != 3 
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 
1
elseif (!$mytype == null && ($PAGE->pagetype == 'site-index' || $PAGE->pagetype =='admin-index')) { 
    return $myclass; 
} 

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

然而额外== null是不必要的,并且基本上是说与If条件用于检查变量if (false == null)if (null == null)

+0

这正是我的想法!我张贴是因为我想知道是否有其他原因。我会给它几天的时间来看看它是否会出现一些极端泄露的PHP怪癖或我的主要误解。 – DogBot

0

!$variableNul升或不

例如

if(!$var) 

意味着如果$var一片空白。

+0

嗯..在我看来,'$ mytype == null'和'!$ mytype == null'是一样的......我没有提到这个代码是在一个函数中。一种检查'$ mytype'是否通过的方法? – DogBot