The difference between and and && operators in PHP

藏色散人
Release: 2023-04-05 06:02:01
Original
4798 people have browsed it


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";
Copy after login

Output:

False
Copy after login

Explanation: 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";
Copy after login

Output:

True
Copy after login

Explanation: 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 ? &#39;TRUE&#39; : &#39;FALSE&#39;), "\n"; 

$bool = TRUE and FALSE; 
  
echo ($bool ? &#39;TRUE&#39; : &#39;FALSE&#39;);
Copy after login

Output:

FALSE 
TRUE
Copy after login

Explanation:

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!