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
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
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;
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)
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)
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));
What is the difference and what are the & and && operators used for?
The key difference is that the & operator is a bitwise operator, while the && operator is a logical operator.
Bitwise operators work on bits and perform "bit by bit" operations, they are applied to the bits of one or two operands. The & represents a bitwise AND operation- where A and B represent two inputs; both inputs must be true in order for C to be true.
So for instance in the example you provided you have:
if(0 & 1){
}
The result of the bitwise AND on 0 and 1 as inputs is 0 (false) because both inputs must be true for the output to be true.
The && operator is a logical operator, which is used to make a decision based on multiple conditions. It can apply to one or two operands each of which may be true or false.
if (!a && b < 5) {
}
The above still evaluates to false because both conditions must be met for the code inside the if statement to be executed. In addition to this, using the && operator will (depending on the language) short circuit, which means that the second condition of the if will only be evaluated if the outcome is not determined by the first condition.
So this expression will fail as soon as a is found to be zero (false) because there is no point in evaluating the second expression as the first expression had to be true. This is another difference as the bitwise & operator does not short circuit.
Related Topics
How to Preview an Image Before and After Upload
PHP Gd Use One Image to Mask Another Image, Including Transparency
PHP - Import CSV File to MySQL Database Using Load Data Infile
PHP How to Import All Classes from Another Namespace
Converting HTML to PDF (Not PDF to HTML) Using PHP
Improve Password Hashing with a Random Salt
Mysqli Binding Params Using Call_User_Func_Array
PHP Error: Function Name Must Be a String
How to Decode Eval( Gzinflate( Base64_Decode(
Using the PHP Http_Accept_Language Server Variable
How to Create Installer for Website. PHP MySQL
How to Force PHP to Use Strings for Array Keys
PHP + SQL Server - How to Set Charset for Connection
Flatten Multidimensional Array Concatenating Keys
Difference Between PHP Echo and Return in Terms of a Jquery Ajax Call