逻辑运算符

表 15-7. 逻辑运算符

例子名称结果
$a and $bAnd(逻辑与)TRUE,如果 $a 与 $b 都为 TRUE
$a or $bOr(逻辑或)TRUE,如果 $a 或 $b 任一为 TRUE
$a xor $bXor(逻辑异或)TRUE,如果 $a 或 $b 任一为 TRUE,但不同时是。
! $aNot(逻辑非)TRUE,如果 $a 不为 TRUE
$a && $bAnd(逻辑与)TRUE,如果 $a 与 $b 都为 TRUE
$a || $bOr(逻辑或)TRUE,如果 $a 或 $b 任一为 TRUE

“与”和“或”有两种不同形式运算符的原因是它们运算的优先级不同(见运算符优先级)。


add a note add a note User Contributed Notes
looris at gmail dot com
18-Jun-2007 02:46
Please note that while you can do things like:
<?php
your_function
() or die("horribly");
?>

you can't do:
<?php
your_function
() or return "whatever";
?>
(it will give you a syntax error).
eduardofleury at uol dot com dot br
14-Jun-2007 01:16
;;;;;;;;;;;;;;;;;;;;;;;;;
; P1 P2; And; OR  ; XOR ;
;;;;;;;;;;;;;;;;;;;;;;;;;
; V  V ; V  ; V  ; F  ;
; V  F ; F  ; V  ; V  ;
; F  V ; F  ; V  ; V  ;
; F  F ; F  ; F  ; F  ;
;;;;;;;;;;;;;;;;;;;;;;;;;

<?php

$a
= 2;
$b = 3;
$c = 6;

print !(
$a > $b && $b < $c);// true

print (($a > $b) and ($b < $c));// false

print ($a == $b or $b < $c); // true

print $a == $b || $b < $c; // true

$x = $a < $b; //$x = true

$y = $b === $c; //$y = false

print  $x xor $y; // true

?>