Solved

php not equal and || logical operator

Posted on 2007-11-30
10
9,855 Views
Last Modified: 2012-06-21
OK, maybe it's late, but this is just driving me bananas.  I'm doing a simple logical operation which took in another program I wrote, but now it's just not seeming to work.  I've simplified my code dramatically to illustrate my issue:

<?php

$foobar = "null";
if (($foobar != "") || ($foobar != "null")) {
    echo "Not Equal";
} else {
    echo "Equal";
}

?>
The above code does not work... It always returns "Not Equal" no matter what $foobar is set to.


Now to drive me even more bananas, I've changed the != to == and it works fine!

$foobar = "null";
if (($foobar == "") || ($foobar == "null")) {
    echo "Equal";
} else {
    echo "Not Equal";
}

Does the PHP != operator have some special thing to get it to work right?  Why am I going nuts over this?  I just know I'm going to pound myself once someone offers the answer...

Thanks...
0
Comment
Question by:con2007
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 21

Expert Comment

by:nizsmo
ID: 20387096
Does this illustrate your problem?

<?php
 
$foobar = null;
if ($foobar != "" || $foobar != null) {
    echo "Not Equal";
} else {
    echo "Equal";
}
 
?>

Open in new window

0
 
LVL 21

Expert Comment

by:nizsmo
ID: 20387099
basically you are assigning "null" as a string, not the actual null value which takes the meaning of empty.
hope this helps.
0
 

Author Comment

by:con2007
ID: 20387104
actually, I needed the null to be the actual value, not a null (bad example on my part).  You can change it to be this and it will still give the same problem:

$foobar = "jojo";
if (($foobar != "") || ($foobar != "jojo")) {
    echo "Not Equal";
} else {
    echo "Equal";
}
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 21

Expert Comment

by:nizsmo
ID: 20387113
This example works fine or does it? please test:
<?php
$foobar = "jojo";
if ($foobar!="jojo" || $foobar=="") 
{
    echo "Not Equal";
} 
else 
{
    echo "Equal";
}
?>

Open in new window

0
 
LVL 21

Accepted Solution

by:
nizsmo earned 300 total points
ID: 20387116
I see the problem with your original example, since it is a ||, only 1 condition has to satisfy!

Your example here:
$foobar = "jojo";
if (($foobar != "") || ($foobar != "jojo")) {
    echo "Not Equal";
} else {
    echo "Equal";
}

first condition $foobar!="" satisfies, so it will always echo "not equal".

Hope this helps.
0
 
LVL 54

Assisted Solution

by:b0lsc0tt
b0lsc0tt earned 200 total points
ID: 20387120
It is late. :)

The results seem correct to me.  The first script is setting $foobar to a string with the word null.  The If is using OR so it just gets to the first part.  The variable isn't empty (i.e. "") so the If is true and you get Not Equal.

The fix is to use and (i.e. &&).  If you want to have both be true then && is needed.

Let me know if you have a question or need more info.

bol
0
 
LVL 11

Expert Comment

by:elfe69
ID: 20387128
Your example will always return "Not Equal" because at least one clause of your OR comparison will always be true:

if $foobar is "jojo", ($foobar != "") is true
if $foobar is "", ($foobar != "jojo") is true
if $foobar is "anything", ($foobar != "") and ($foobar != "jojo") are true
0
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 20387131
To answer the second part of your question the fact that you test for Not Equal (i.e. !=) does make a difference, especially when the test uses OR.  In the case of Equal then Or works better (equals this OR that).  However when you use Not Equal then you have to be more cautious about the test.  You can't just switch the two (equal/not and &&/||) and expect the results to just be "opposite."

Let me know if you have a question.

bol
0
 

Author Comment

by:con2007
ID: 20387134
DOH! I knew it was going to be something ridiculous on my part!  Yes, it needs && not ||.

Thanks all!!
0
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 20387159
Your welcome!  I'm glad I could help.  Thanks for the grade, the points and the fun question.

bol
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

791 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question