Home  >  Article  >  Backend Development  >  What is the usage of and in PHP

What is the usage of and in PHP

WBOY
WBOYOriginal
2022-05-05 17:22:583460browse

In PHP, and is used for logical judgment. The syntax is "value 1 and value 2". If the operands on both sides of and are true, then return true, otherwise return false; and is a logical operator , like the "&&" operator, both represent logical "AND", and the "&&" operator has a higher priority than and.

What is the usage of and in PHP

The operating environment of this article: Windows 10 system, PHP version 7.1, Dell G3 computer

What is the usage of and 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: False

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";

Output: True

Explanation: Since variable $a = 100 and another variable $b = 10, the calculation result of condition $a == 100 is 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 because AND logic states that the AND operation will be true only if both operands are true. But when $b = 20 is input, the condition pow($b,2)==$a is false, so the AND operation result is false.

Comparison between 'AND' and '&&' operators:

Based on precedence:

Priority basically determines which operations are performed first in an expression. The '&&' operator has high precedence, and the 'AND' operator has low precedence.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of What is the usage of and in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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