?
Solved

php not equal and || logical operator

Posted on 2007-11-30
10
Medium Priority
?
9,875 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
7 Extremely Useful Linux Commands for Beginners

Just getting started with Linux? Here's a quick start guide that has 7 commands that we believe will come in handy.

 
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 1200 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 800 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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to count occurrences of each item in an array.
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…
Suggested Courses

770 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