‘AND’ vs ‘&&’ as operator in PHP
‘AND’ Operator
The AND operator is called logical operator. It returns true if both the operands are true.
Example:
<?php // Variable declaration and // initialization $a = 100; $b = 50; // Check two condition using // AND operator if ($a == 100 and $b == 10) echo "True"; else echo "False"; ?> |
False
Explanation: Since variable $a = 100 and another variable $b = 10, the condition $a == 100 evaluates to true and $b == 10 also evaluates to true. Therefore, ‘$a == 100 and $b == 10’ evaluates to true because AND logic states that if both the operands are true, then result also be true. But when the input $b = 20, the condition $b == 10 is false, so the AND operation result will be false.
‘&&’ Operator
The && operator is called logical operator. It returns true if both operands are true.
Example:
<?php // Declare a variable and initialize it $a = 100; $b = 10; // Check the condition if ($a == 100 && pow($b, 2) == $a) echo "True"; else echo "False"; ?> |
True
Explanation: Since variable $a = 100 and another variable $b = 10, the condition $a == 100 evaluates to true and pow($b, 2) == $a also evaluates to true because $b = 10 raised to the power of 2 is 100 which is equal to $a. Therefore, ‘$a == 100 && pow($b, 2) == $a’ evaluates to true as AND logic states that only when both the operands are true, the AND operation result is true. But when the input $b = 20, the condition pow($b, 2) == $a is false, so the AND operation result is false.
Comparison between ‘AND’ and ;&&’ operator: There are some difference between both operator are listed below:
- Based on Precedence: Precedence basically decides which operations are performed first in an expression. The precedence of ‘&&’ operator is high and the precedence of ‘AND’ operator is low.
- Based on operation:
Example:<?php// Expression to use && operator$bool= TRUE && FALSE;// Display the result of && operationecho($bool?'TRUE':'FALSE'),"\n";// Expression to use AND operator$bool= TRUEandFALSE;// Display the result of AND operationecho($bool?'TRUE':'FALSE');?>chevron_rightfilter_noneOutput:FALSE TRUE
Explanation:
The result of both operator is different whenever operands are same. The first expression evaluates to FALSE while the second expression evaluates TRUE even though both are using the same operation.- The first expression, $bool = TRUE && FALSE; evaluates to FALSE because first && operation is performed, then the result to assigned to the variable $bool because precedence of && operator is higher than the precedence of =.
- The second expression, $bool = TRUE and FALSE; evaluates to TRUE because the operator “and” has lower precedence than the operator “=” so the value TRUE which is on the right of = is assigned to $bool and then the “and” operation is performed internally but is not assigned, therefore $bool now holds TRUE.
So to explain, the fundamental difference in the AND operator and the && operator is their precedence difference but both perform the same operation.
Recommended Posts:
- Why overriding both the global new operator and the class-specific operator is not ambiguous?
- Ternary operator vs Null coalescing operator in PHP
- PHP | $ vs $$ operator
- new vs operator new in C++
- set operator= in C++ STL
- unordered_multiset operator = in C++ STL
- PHP 7 | Spaceship Operator
- What is the !! (not not) operator in JavaScript?
- unordered_set == operator in C++ STL
- unordered_set operator!= in C++ STL
- Double not (!!) operator in PHP
- unordered_set operator= in C++ STL
- What is the difference between the | and || or operator in php?
- Types of Operator Overloading in C++
- C++ | Nested Ternary Operator
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



