Differences in the use of PHP logical operators & and && as well as && and ||

伊谢尔伦
Release: 2023-03-10 22:46:01
Original
5956 people have browsed it

Logical operators are nothing more than performing logical operations on values. (&&) or (||)" These two operators can speed up the running speed of PHP code in the program.

First look at a piece of code:

<?php   
    $test="李四";   
    $test=="张三"&&$test="张三来了";   
    echo $test;  //输出“李四”   
    $test="李四";   
    $test=="张三"||$test="张三不在这里";   
    echo $test;  //输出“张三不在这里”   
?>
Copy after login

Why Will this result occur? If we follow the usual method, we must at least use an IF statement to determine. But now only two logical operations will change the value of the variable. Let's analyze its operation principle.

In both sides of the expression involved in the logical operation, operations are performed from left to right, and as long as one of the "AND" operations is false, the result of the entire expression will be. is false. Therefore, when the expression on the left is false, there is no need to perform the operation. This processing is undoubtedly beneficial to the running efficiency of the program. So as the title says, it is an efficient usage. Logical OR is different: as long as one is true, the entire expression is true. Therefore, if the left side is false, the expression on the right must be judged. If you understand or understand the above, you will not be aware of the result. It feels strange.

The above example can of course be realized through conditional judgment statements. The current situation is to reduce the amount of code and, most importantly, to increase the execution efficiency of the program. The key to grasping this is that the expression runs from left to right. It stops when the first value determines the value of the entire expression. It is worth mentioning that the right side can be an expression or an expression. Function, but it cannot be a series of statement combinations or output statements. After all, it is still a component of a logical expression.

For the "and" (&&) operation: x && y When x is false, directly. Skip, do not execute y;
For the "or" (||) operation: x||y When x is true, skip directly, do not execute y in & and && in php. Similarities and Differences

<?php
$a=10;
if($a>4 && (++$a>10))
{
}
//输出结果为11.
echo $a;
?>
<?php
$a=10;
if($a>4 and (++$a>10))
{
}
//输出结果为11.
echo $a;
?>
**************************************************************
<?php
$a=10;
if($a>4 && (++$a<10))
{
}
//输出结果为11.
echo $a;
?>
<?php
$a=10;
if($a>4 & (++$a<10))
{
}
//输出结果为11.
echo $a;
?>
*********************************************************
<?php
$a=10;
if($a<4 && (++$a>10))
{
}
//输出结果为10.
echo $a;
?>
<?php
$a=10;
if($a<4 & (++$a>10))
{
}
//输出结果为11.
echo $a;
?>
*******************************************************************
<?php
$a=10;
if($a<4 && (++$a<10))
{
}
//输出结果为10.
echo $a;
?>
<?php
$a=10;
if($a<4 & (++$a<10))
{
}
//输出结果为11.
echo $a;
?>
*******************************************************************
<?php
// 下面的 sktest() 不被调用,原因是它们被运算符“短路”。
$a = (false && sktest());
$b = (true  || sktest());
$c = (false and sktest());
$d = (true  or  sktest());
// "||" 的优先级比 "or" 高
$e = false || true; // $e 被赋值为 (false || true),结果为 true
$f = false or true; // $f 被赋值为 false [Altair注:"=" 的优先级比 "or" 高]
var_dump($e, $f);
// "&&" 的优先级比 "and" 高
$g = true && false; // $g 被赋值为 (true && false),结果为 false
$h = true and false; // $h 被赋值为 true [Altair注:"=" 的优先级比 "and" 高]
var_dump($g, $h);
?>
Copy after login

The above is the detailed content of Differences in the use of PHP logical operators & and && as well as && and ||. 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