Don’t use “and” and “or” logical operator in PHP, it has flaw
- Tuesday, June 17, 2008, 5:12
- php, tips and technique
- 11 comments
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: 5%
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






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
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) …
@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…
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 !
$return_val = (false or true);
var_dump($return_val); //prints bool(false)
$return_val = (true and false);
var_dump($return_val); //prints bool(true)
the above commented result is wrong, i think u got the funda.
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….
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
ya U saw that link…as you can see you can see the undesired result due to precedence of the operator…
maybe this flaw in individually release of php