Reputation: 528
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;
}`
Upvotes: 1
Views: 1761
Reputation: 33542
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
Upvotes: 4
Reputation: 1575
!$variable
used with If
condition to check variable is Nul
l or Not
For example
if(!$var)
means if $var
IS null.
Upvotes: 0
Reputation: 3484
!
before variable negates it's value
this statement is actually same as !$mytype
since FALSE == NULL
!$mytype == null
statement will return TRUE
if $mytype
contains one of these:
TRUE
Upvotes: 2
Reputation: 10057
!
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
Upvotes: 3
Reputation: 1447
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)
Upvotes: 1