Not (!)

[b]code1[/b]

<?php 
$myVar=[COLOR="red"]1[/COLOR];
if (!$myVar==[SIZE="5"]0[/SIZE])
{
echo $myVar; 
}
?>
//if $myVar is not 0, then print $myVar.

The code above print 1. It works fine.

[b]code2[/b]

<?php
$myVar=[COLOR="red"]1[/COLOR];
if (!$myVar==[SIZE="5"]2[/SIZE])
{
echo $myVar; 
}
?>

//if $myVar is not 2, then print $myVar.

I am expecting that the code above print 1.
But it doesn’t print 1.

What’s wrong in the code2?
How can I correctly write for the meaning of “if $myVar is not 2, then print $myVar.”?

if (!$myVar==2) is not “if $myVar does not equal 2”, it is “if !$myVar equals 2”, that is to say “if the boolean negation of $myVar equals 2”.
Since $myVar is 1, which has a boolean value of true, the boolean negation of $myVar is false. Since false doesn’t equal 2 the condition in the if in your second snippet doesn’t validate to true.

What you seem to want is

if ($myVar != 2)

:slight_smile:

You could throw some parentheses around it though.


<?php
$var = 1;

var_dump(! ($var == 2) ); #true

Thank you, ScallioXTX.

Thank you, AnthonySterling.

I think the problem is your “2” is too big :smiley:

This will also be useful.