逻辑问题
除了上述我大量使用的比较操作符外,PHP也提供了四个逻辑操作符,该四个逻辑操作符经设计以将条件表达式聚合在一起。在下面的例子中说明该四个操作符:逻辑与(Logical AND)、逻辑或(Logical OR)、逻辑异或(Logical XOR)、逻辑非(Logical NOT):
<?php /* define some variables */ $auth = 1; $status = 1; $role = 4; /* logical AND returns true if all conditions are true */ // returns true $result = (($auth == 1) && ($status != 0)); print "result is $result<br />"; /* logical OR returns true if any condition is true */ // returns true $result = (($status == 1) || ($role <= 2)); print "result is $result<br />"; /* logical NOT returns true if the condition is false and vice-versa */ // returns false $result = !($status == 1); print "result is $result<br />"; /* logical XOR returns true if either of two conditions are true, */
/* or returns false if both conditions are true */ // returns false $result = (($status == 1) xor ($auth == 1)); print "result is $result<br />"; ?>
逻辑操作符在建构条件语句中起着重要的作用,因为其可用于将相关的条件简单并极好的联结在一起。参看逻辑操作符如何使用的更多例子,请参阅http://www.php.net/manual/en/language.operators.logical.php。