1

What does an exclamaton mark in front of a variable mean? And how is it being used in this piece of code?

EDIT: From the answers so far I suspect that I also should mention that this code is in a function where one of the parameters is $mytype ....would this be a way of checking if $mytype was passed? - Thanks to all of the responders so far.

 $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;
    }`
4

5 に答える 5

4

The exclamation mark in PHP means not:

if($var)

means if $var is not null or zero or false while

if(!$var)

means if $var IS null or zero or false.

Think of it as a query along the lines of:

select someColumn where id = 3

and

select someColumn where id != 3
于 2012-09-08T14:17:16.110 に答える
3

! negates the value of whatever it's put in front of. So the code you've posted checks to see if the negated value of $mytype is == to 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
于 2012-09-08T14:18:08.580 に答える
2

!変数がその値を否定する前

このステートメントは実際に!$mytypeFALSE == NULL

!$mytype == null

次のいずれかが含まれているTRUE場合、ステートメントが返されます。$mytype

  • TRUE
  • ゼロ以外の数
  • 空でない文字列
于 2012-09-08T14:26:10.120 に答える
1
elseif (!$mytype == null && ($PAGE->pagetype == 'site-index' || $PAGE->pagetype =='admin-index')) {
    return $myclass;
}

The above !$mytype == null is so wrong. !$mytype means that if the variable evaluates to false or is null, then the condition will execute.

However the extra == null is unnecessary and is basically saying if (false == null) or if (null == null)

于 2012-09-08T14:20:05.827 に答える
0

!$variable used with If condition to check variable is Null or Not

For example

if(!$var)

means if $var IS null.

于 2012-09-08T14:22:48.290 に答える