Don’t use “and” and “or” logical operator in PHP, it has flaw

Today, When I was doing some programming stuffs and I found out that there is serious flaw in the “and” , “or” logical operator of PHP. I’m not talking about the symbol “||” and “&&” logical operator. I’m talking about the “and” and “or” logical operator (operator with words).


Example of the flaw of using “and” and “or” logical operator

Look at the following example and notice the output of this example,

$return_val = false or true;
var_dump($return_val);  //prints bool(false)

$return_val = true and false;
var_dump($return_val);  //prints bool(true)

As you can see in the first example, “bool(false)” is the output in the browser. I don’t need to tell you that “false or true” is always true and there is no doubt about this. But look at the output, what a freaking output by the “or” operator of PHP.

And now, just look at the second example, you’ll see “bool(true)” as a output to the browser. What a ridiculous result? How can “true and false” can be true, it must be “false” without any doubt.

But, you’ll not get such a kind of rediculous result with “||” and “&&” logical operator.

14 thoughts on “Don’t use “and” and “or” logical operator in PHP, it has flaw

  1. Cool Stuff..
    Good that I never used them as far as I can remember…

    I dug this Story..

  2. Priotiry of the OR operator is less then =.
    So $return_val = false or true; will be evaluated as: ($return_val = false) or true;
    OR is usefull when you need to write something like this:
    $var = $var1 or $var = $var2 or $var = $var3;
    which is a shortcut to:
    if ($var1) $var = $var1 elseif ($var2) …

  3. @Bijay – Thanks man…I’ve just using it and got this undesired result and wanted alert everyone..

    @Alexa – thanks for explanation…a beginner developer may really be in dilemma if they use the example in the way I did..the priority of “and” , “or” operator is really awful compared to assignment operator…

  4. khalid

    Use logical AND and OR operator always in brackets ( ) , it will give you the proper result.
    PHP does not have any flaws regarding it.

    Your code should be like this :

    Enjoy !

  5. khalid

    $return_val = (false or true);
    var_dump($return_val); //prints bool(false)

    $return_val = (true and false);
    var_dump($return_val); //prints bool(true)

  6. khalid

    the above commented result is wrong, i think u got the funda.

  7. hey Khalid….which PHP version u’re using? I’m using PHP5 and the above result is absolutely correct, I’ve tested it 3-4 times in PHP5…
    better check your result once man….

  8. khalid

    My PHP version is “5.2.6”.
    We need to go through the below link.

  9. ya U saw that link…as you can see you can see the undesired result due to precedence of the operator…

  10. jimmy

    maybe this flaw in individually release of php

  11. Matt

    The logical operators work fine in PHP when you use sound logic to test them. I use them all the time. As Khalid said, not understanding precedence is not a “flaw” in php, it is a lack of understanding on your part. Khalid should have updated his “prints” comments on the code he posted like so.

    $return_val = (false or true);
    var_dump($return_val); //prints bool(true)

    $return_val = (true and false);
    var_dump($return_val); //prints bool(false)

    Again, basic logic… not a flaw… I hope you have not confused the crap out of anyone else with this.

  12. Great Articles here…
    I will be a regular user of this site :)


  13. Pooja

    The basic logic for using the logical operators in any of language especially works same as the semiconductor Gates.If anyone who is aware of semiconductors gates ,then he/she can truely can say that this is not an flaw.
    this is the And Gate truth table
    A B A AND B
    0 0 0
    0 1 0
    1 0 0
    1 1 1

    and this is the or gate truth table

    A B A OR B
    0 0 0
    0 1 1
    1 0 1
    1 1 1

    if 1 is true and 0 is false
    then tracing accordingly ,will find that PHP returns the correct answer.So I guess,its not a flaw…..

Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>