Jump to content

[SOLVED] IF and using one or more logical operators?


ifubad

Recommended Posts

$a = "b";

if ($a != "b" || $a != "c") {
    echo "not equal";
}

 

I alway get mixed up on using one or more logical operators with an IF statement

 

$a is equal to "b" and should not echo. But it seems to be executing the second test when it should not. What am I doing wrong?

That "if" statement is saying

 

echo "not equal" if $a is not "b" or $a is not "c"

 

That is true, so the echo is being executed.

 

If you don't want it to echo, you want to use the "&&" operator

<?php
$a = "b";

if ($a != "b" && $a != "c") {
    echo "not equal";
}?>

Ken

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.