Solved

php not equal and || logical operator

Posted on 2007-11-30
10
9,846 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
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…
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 …

914 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

15 Experts available now in Live!

Get 1:1 Help Now