PHP offers incredible operators to perform operations such as arithmetic, assignment, comparison and many more ...In this article, more importance will be laid on logical operators "&&" and "AND" and will study how they can be utilized based on their precedence. Logical operators "&&" and "AND" produce true-or-false as results, and therefore these are also known as Boolean operators.
Before diving into deep let's learn what is "AND" operator? "AND" operator returns true if and only if both the conditions are true. Let's take an example to demonstrate "AND" operator.
<?php $val1 = 20; $val2 = 50; if ($val1 == 20 and $val2== 50) echo "True"; else echo "False"; ?>
Since variable $val1 = 20 and $val2 = 50, the condition $val1 == 20 evaluates to true and $val2 == 50 also evaluates to true. Therefore,both the operands are true, the result also be true Let's discuss another example.
<?php $a = 30; $b = 50; if ($a == 30 and $b == 10) echo "True"; else echo "False"; ?>
Since we have declare variable $a = 30 and $b = 50, the condition $a == 30 evaluates to true but But when the input $b = 50 in declaration and in the condition $b == 10 which returns false, so the AND operation result will be false.
Now comes to "&&" operator, it also works the same as "AND" operator, it returns true if both the conditions/operands are true in the expression. Now let's have a look at the below example which implements "&&" operators.
<?php $val1 = 25; $val2 = 5; if ($val1 == 25 && pow($val2, 2) == $val1) echo "True"; else echo "False"; ?>
Since variable $val1 = 25 and $val2 = 5, the condition $val1 == 25 evaluates to true and pow($val2, 2) == $val1 likewise evaluates to true because $val2= 5 raised to the power of 2 is 25 which is equal to $val1. Therefore, '$val1 == 25&& pow($val2, 2) == $val1' evaluates to true as AND logic states that only when both the operands are true, the AND operation result is true.
Let's take another case,If we declare the input $val2 = 20, the condition pow($val2, 2) == $val1 will result false, so the AND operation result will be false.
Now discuss the comparison between "AND" and "&&" operator in terms of their precedence. The operations are performed according to the precedence of operators in an expression. The priority of '&&' operator is high and the priority of "AND" operator is low. Let's study the above difference through the below example.
<?php $a =10; $b = NULL; $val = $a && $b; echo ($val ? 'TRUE' : 'FALSE'),"\n"; $val = $a and $b; echo ($val ? 'TRUE' : 'FALSE'); ?>
The result of both operators is different whenever operands are the same. The first expression articulates to FALSE while the second expression articulates TRUE despite the fact that both are utilizing a similar activity.
The first expression, $val = $a && $b; articulates to FALSE because first "&&" operation is performed,then the outcome assigned to the variable $val because priority of && operator is higher than the priority of "=" operator .
The second expression, $bool = $a and $b; evaluates to TRUE because the operator “and” has lower priority than the operator "=" so the value of variable $a = 10 which is on the right of = is assigned to $val, so $val holds 10 and then the "and" operation is performed internally but is not assigned, therefore $val now holds TRUE.
So to clarify, the principal difference between the "AND" operator and the "&&" operator is their precedence yet both perform a similar activity.