Home > Backend Development > PHP Tutorial > php logical operators

php logical operators

伊谢尔伦
Release: 2016-11-24 13:27:40
Original
1579 people have browsed it

Logical Operator

Example                                                                                                                      b And (logical AND) TRUE if $a and $b are both TRUE. $a or $b Or (logical OR) TRUE, if either $a or $b is TRUE. $a xor $b Xor (logical exclusive OR) TRUE, if either $a or $b is TRUE, but not both at the same time.

! $a Not (logical not) TRUE, if $a is not TRUE.

$a && $b And (logical AND) TRUE, if $a and $b are both TRUE.

$a || $b Or (logical OR) TRUE, if either $a or $b is TRUE. ​

The reason why "AND" and "OR" have two different forms of operators is that the priorities of their operations are different (see Operator Priority).

Example #1 Logical Operator Example

<?php
// --------------------
// foo() 根本没机会被调用,被运算符“短路”了
$a = (false && foo());
$b = (true  || foo());
$c = (false and foo());
$d = (true  or  foo());
// --------------------
// "||" 比 "or" 的优先级高
// 表达式 (false || true) 的结果被赋给 $e
// 等同于:($e = (false || true))
$e = false || true;
// 常量 false 被赋给 $f,true 被忽略
// 等同于:(($f = false) or true)
$f = false or true;
var_dump($e, $f);
// --------------------
// "&&" 比 "and" 的优先级高
// 表达式 (true && false) 的结果被赋给 $g
// 等同于:($g = (true && false))
$g = true && false;
// 常量 true 被赋给 $h,false 被忽略
// 等同于:(($h = true) and false)
$h = true and false;
var_dump($g, $h);
?>
Copy after login

The output of the above routine is similar to:

bool(true)
bool(false)
bool(false)
bool(true)
Copy after login

Related labels:
php
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