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.

Popularity: 4% [?]

Tags: ,

Enter your email address and get free tutorials, tips and tricks of PHP, Ajax, JavaScript and CSS directly delivered to you email inbox:

Related Posts

» Some unusual comparison operations in PHP
» Upgrade your wordpress – easily and quickly
» Clean and efficient coding technique in PHP
» Parsing the XML in easy way using PHP

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

  • Bijay Rungta wrote on 17 June, 2008, 10:28

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

    I dug this Story..

    http://digg.com/programming/Flaw_in_and_and_or_logical_operator_in_PHP

  • Alex@Net wrote on 17 June, 2008, 11:58

    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) …

  • Roshan wrote on 17 June, 2008, 16:18

    @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…

  • khalid wrote on 18 June, 2008, 21:04

    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 !

  • khalid wrote on 18 June, 2008, 21:06

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

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

  • khalid wrote on 18 June, 2008, 21:12

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

  • Roshan wrote on 19 June, 2008, 4:33

    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….

  • khalid wrote on 19 June, 2008, 5:18

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

    http://in.php.net/manual/en/language.operators.logical.php

  • Roshan wrote on 21 June, 2008, 15:20

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

  • jimmy wrote on 31 December, 2008, 6:00

    maybe this flaw in individually release of php

  • Matt wrote on 13 March, 2009, 14:34

    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.

Trackbacks

  1. What The Heck Is PHP? : ProfitableSistas

Write a Comment

 


Copyright © 2009 Roshan Bhattarai's Blog. All rights reserved.
Powered by WordPress.org, Custom Theme and ComFi.com Calling Card Company.