Solved

php not equal and || logical operator

Posted on 2007-11-30
10
9,857 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

730 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