Jun
25
2011

PHP if statement with multiple not equal conditions

I like to think of this one as AND OR confusion. This is based on actual code found within Sugar CRM so if it threw you for a loop your not alone. I have reported the bug to them, however as of 6/25/2011 it still has not been addressed. Now lets make sure we do not fall victim to this.

Ok this is a simple one however I cannot tell you how many times this has thrown me for a loop. Lets say you have a simple IF statement and you are checking if a variable is not equal to a value OR if it is not equal to another value.


// OR example

$param = 'redirect_url';

if($param != 'redirect_url' || $param != 'submit') {
	echo 'true';
}else{
	echo 'false';
}

// will echo true

The above results are surprising as $param is equal to redirect_url and we are asking if $param is not equal to x OR if $param is not equal to y, well it is not equal to one and equal to another so you would assume it return false.

Now lets look at && (and) with != comparison operator


//AND example with != comparison operator

$param = 'redirect_url';

if($param != 'redirect_url' && $param != 'submit') {
	echo 'true';
}else{
	echo 'false';
}

// will echo false

Now lets look at && (and) with <> comparison operator


//AND example with <> comparison operator

$param = 'redirect_url';

if($param <> 'redirect_url' && $param <> 'submit') {
	echo 'true';
}else{
	echo 'false';
}

// will echo false

And my favorite solution !in_array()


//in_array example

$param = 'redirect_url';
$bad = array(
	'redirect_url',
	'submit'
);

if(!in_array($param,$bad)) {
	echo 'true';
}else{
	echo 'false';
}

// will echo false

I have spoken to many PHP gurus and every time the results have surprised them as they did me the first time I ran across this way back in 2008.

Grab the code below and be prepared to amaze your friends, just make sure to bet them a beer first!

<pre>
$param = 'redirect_url';

if($param != 'redirect_url' || $param != 'submit') {
	echo 'true';
}else{
	echo 'false';
}
// will echo true
</pre>

<?php

// OR example

$param = 'redirect_url';

if($param != 'redirect_url' || $param != 'submit') {
	echo 'true';
}else{
	echo 'false';
}
// will echo true
?>

<pre>
$param = 'redirect_url';

if($param != 'redirect_url' && $param != 'submit') {
	echo 'true';
}else{
	echo 'false';
}
// will echo false
</pre>

<?php

//AND example with != comparison operator

$param = 'redirect_url';

if($param != 'redirect_url' && $param != 'submit') {
	echo 'true';
}else{
	echo 'false';
}
// will echo false
?>

<pre>
$param = 'redirect_url';

if($param <> 'redirect_url' && $param <> 'submit') {
	echo 'true';
}else{
	echo 'false';
}
// will echo false
</pre>

<?php

//AND example with <> comparison operator

$param = 'redirect_url';

if($param <> 'redirect_url' && $param <> 'submit') {
	echo 'true';
}else{
	echo 'false';
}
// will echo false
?>

<pre>
$param = 'redirect_url';
$bad = array(
	'redirect_url',
	'submit'
);

if(!in_array($param,$bad)) {
	echo 'true';
}else{
	echo 'false';
}
// will echo true
</pre>

<?php

//in_array example

$param = 'redirect_url';
$bad = array(
	'redirect_url',
	'submit'
);

if(!in_array($param,$bad)) {
	echo 'true';
}else{
	echo 'false';
}
// will echo false
?>

10 Comments + Add Comment

  • thanks this solved my problem
    bug in php HMMM

    $param = ‘redirect_url';
    $bad = array(
    ‘redirect_url’,
    ‘submit’
    );

    if(!in_array($param,$bad)) {
    echo ‘true';
    }else{
    echo ‘false';
    }

    Thumb up 0 Thumb down 0

  • $a = 2;

    if ($a >10)

    {

    echo “exess qauntity” . $a;

    }

    else

    {

    if ($a ==10)

    {

    echo “eqaul qauntity ” . $a;

    }

    if ($a

    Thumb up 0 Thumb down 0

    • Thanks

      Thumb up 0 Thumb down 0

  • I fail to see what’s surprising or confusing about these results.

    Thumb up 4 Thumb down 0

    • Dustin as you read through the article the only result that seems out of the ordinary is:

      $param = 'redirect_url';
      if($param != 'redirect_url' || $param != 'submit') {
          echo 'true';
      }else{
          echo 'false';
      }
      

      Thumb up 0 Thumb down 3

  • Like a pet owner, you may put your dog to work assisting you to shed weight.
    You can also combine different forms of cardiovascular exercises
    to keep yourself away from boredom. Here’s another question, would
    you order the same food you order in the drive-thru that you would order if you had to park and walking
    into the restaurant, order in person.

    Thumb up 0 Thumb down 0

  • Thanks for nice support.

    Thumb up 0 Thumb down 0

  • This rules we used our organization.

    Thumb up 0 Thumb down 0

  • First of all thank you for sharing this informative blog.. This blog having more useful information that information explanation are step by step and very clear so easy and interesting to read.. After reading this blog i am strong in this topic which helpful to cracking the interview easily..

    best php training institute

    Thumb up 0 Thumb down 0

Leave a comment

Show/Hide Footer Actions

Status: Available for your project.