Home > Article > Backend Development > The difference between and and && operators in PHP
##The 'and' operator in PHP is called a logical operator. If both operands are true, return true.
Example:<?php // 变量声明 // 初始化 $a = 100; $b = 50; if ($a == 100 and $b == 10) echo "True"; else echo "False";Output:
FalseExplanation: Since variable $a = 100 and another variable $b = 10, the condition $a == 100 is calculated The result is true, and $b == 10 evaluates to true. Therefore, '$a == 100 and $b == 10' evaluates to true because AND logic says that if both operands are true, the result is also true. But when $b = 20 is input, the condition $b == 10 is false, so the AND operation result will be false.
The ‘&&’ operator in PHP is also called a logical operator. If both operands are true, return true.
Example:<?php // 声明一个变量并初始化 $a = 100; $b = 10; if ($a == 100 && pow($b, 2) == $a) echo "True"; else echo "False";Output:
TrueExplanation: Since variable $a = 100 and another variable $b = 10, the calculation result of condition $a == 100 is true, while 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 because AND logic states that the AND operation results in true only if both operands are true. But when $b = 20 is entered, the condition pow($b,2)==$a is false, so the AND operation results in false.
Comparison between 'AND' and '&&' operators:
Based on precedence: Priority is basically determined in the expression Which actions are performed first in . The '&&' operator has high precedence, and the 'AND' operator has low precedence. Based on operation: Example:<?php // 使用&&运算符的表达式 $bool = TRUE && FALSE; // 显示&&操作的结果 echo ($bool ? 'TRUE' : 'FALSE'), "\n"; $bool = TRUE and FALSE; echo ($bool ? 'TRUE' : 'FALSE');Output:
FALSE TRUEExplanation: Whenever the operands are the same, two The results of the operators are different. The first expression evaluates to FALSE, while the second expression evaluates to TRUE, even though they both use the same operation. So, the fundamental difference between the AND operator and the && operator is their precedence difference, but both perform the same operation. The first expression, $bool = TRUE && FALSE; evaluates to FALSE because the first && operation is performed, and then the result is assigned to the variable $bool, because the && operator has high priority. Priority of =. The second expression, $bool = TRUE and FALSE; evaluates to TRUE because the operator "and" has a lower priority than the operator "=", so the value TRUE on the right side of = is assigned to $bool, then the "and" operation is performed internally but not assigned, so $bool now remains TRUE. This article is a detailed introduction to the 'and' and '&&' operators in PHP. I hope it will be helpful to friends in need!
The above is the detailed content of The difference between and and && operators in PHP. For more information, please follow other related articles on the PHP Chinese website!