PHP : Difference between '&&' and 'AND'
They do the same thing, but &&
has higher precedence than AND
.
http://php.net/manual/en/language.operators.precedence.php
Difference between & and && in PHP
&
is bitwise AND. See Bitwise Operators. Assuming you do 14 & 7
:
14 = 1110
7 = 0111
---------
14 & 7 = 0110 = 6
&&
is logical AND. See Logical Operators. Consider this truth table:
$a $b $a && $b
false false false
false true false
true false false
true true true
AND' vs '&&' as operator
If you use AND
and OR
, you'll eventually get tripped up by something like this:
$this_one = true;
$that = false;
$truthiness = $this_one and $that;
Want to guess what $truthiness
equals?
If you said false
... bzzzt, sorry, wrong!
$truthiness
above has the value true
. Why? =
has a higher precedence than and
. The addition of parentheses to show the implicit order makes this clearer:
($truthiness = $this_one) and $that
If you used &&
instead of and
in the first code example, it would work as expected and be false
.
As discussed in the comments below, this also works to get the correct value, as parentheses have higher precedence than =
:
$truthiness = ($this_one and $that)
Is there any difference between and and && operators in PHP?
In your case, &&
and and
do the same thing, but take a look at the operator precedence. You can see that &&
and and
are not on the same level, so mixing that could give you unexpected results in some cases - I recommend always using &&
, but that's your choice.
Difference between AND [and] &&
On their own, they do exactly the same. So a && b
means the same than a and b
. But, they are not the same, as &&
has a higher precedence than and
. See the docs for further information.
This example here shows the difference:
// The result of the expression (false && true) is assigned to $e
// Acts like: ($e = (false && true))
$e = false && true;
// The constant false is assigned to $f and then true is ignored
// Acts like: (($f = false) and true)
$f = false and true;
Difference between && and and : Operator precedence and short circuiting
The code
$e = isset($a) and isset($b);
is parsed the same as
($e = isset($a)) and isset($b);
Therefore $e
, as determined by isset($a)
and the assignment, is true
- independent of evaluating isset($b)
.
PHP operator difference && and and
The reason is operator precedence. Among three operators you used &&
, and
& =
, precedence order is
&&
=
and
So $a
in your program calculated as expected but for $b
, statement $b = isset($one)
was calculated first, giving unexpected result. It can be fixed as follow.
$b = (isset($one) and isset($two));
PHP: OR and ||, AND and &&
There is no difference at all, except that perhaps using AND
is somewhat more readable.
EDIT: just checked, and there is a small difference in the operators precedence, see http://php.net/manual/en/language.operators.precedence.php
php operators && vs AND, || vs OR
"||" has a greater precedence than "or"
"&&" has a greater precedence than "and"
From php manual, can read the full article here (Exemple1)
Related Topics
Merge a Table and a Change Log into a View in Postgresql
Laravel 5.3 - HTMLspecialchars() Expects Parameter 1 to Be String
Extension PHP5 Does Not Parse in Xampp
Date Function to Display All Dates Between Two Dates
Trouble Passing Variables to PHP Script on Image Upload
Differences Between a While Loop and a for Loop in PHP
Split String into Equal Parts Using PHP
How to Start a Windows Gui Program Using PHP
How to Subtract 24 Hour from Date Time Object in PHP
How to Use File_Get_Contents or File_Get_Html
Does the Use Keyword in PHP Closures Pass by Reference
PHP - How to Know If Server Allows Shell_Exec
When Should I Use Closecursor() for Pdo Statements
How to Load the Requested Language File: Language/En/Form_Validation_Lang.Php