Difference between OR and || in PHP
Based on behavior OR and || are same,
$a = 1;
$b = 1;
if($a==$b || $a=='5') {
echo "here";
} // OUTPUT: here
if($a==$b || $a=='5') {
echo "here";
} // OUTPUT: here
//////////////////////////////////////////////////
BUT, "OR" operator have lower precedence than ||
Because 'OR' have lower priority than '=' but '||' have higher priority. Same thing apply to "and" and && operator.
Let's check difference,
<?php
$bool = true && false;
var_dump($bool); // false, that's expected
$bool = true and false;
var_dump($bool); // true, ouch!
?>
Please refer below link for more detail,
http://php.net/manual/en/language.operators.precedence.php
So use only ("||" , "&&") operator instead of ("or","and") operator.
Enjoy your coding!!!
Based on behavior OR and || are same,
$a = 1;
$b = 1;
if($a==$b || $a=='5') {
echo "here";
} // OUTPUT: here
if($a==$b || $a=='5') {
echo "here";
} // OUTPUT: here
//////////////////////////////////////////////////
BUT, "OR" operator have lower precedence than ||
Because 'OR' have lower priority than '=' but '||' have higher priority. Same thing apply to "and" and && operator.
Let's check difference,
<?php
$bool = true && false;
var_dump($bool); // false, that's expected
$bool = true and false;
var_dump($bool); // true, ouch!
?>
Please refer below link for more detail,
http://php.net/manual/en/language.operators.precedence.php
So use only ("||" , "&&") operator instead of ("or","and") operator.
Enjoy your coding!!!
No comments:
Post a Comment