Solved

php not equal and || logical operator

Posted on 2007-11-30
10
9,842 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 21

Expert Comment

by:nizsmo
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 54

Assisted Solution

by:b0lsc0tt
b0lsc0tt earned 200 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Your welcome!  I'm glad I could help.  Thanks for the grade, the points and the fun question.

bol
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

771 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now