例子 | 名称 | 结果 |
---|---|---|
$a and $b | And(逻辑与) | TRUE ,如果 $a 和 $b 都为 TRUE 。 |
$a or $b | Or(逻辑或) | TRUE ,如果 $a 或 $b 任一为 TRUE 。 |
$a xor $b | Xor(逻辑异或) | TRUE ,如果 $a 或 $b 任一为 TRUE ,但不同时是。 |
! $a | Not(逻辑非) | TRUE ,如果 $a 不为 TRUE 。 |
$a && $b | And(逻辑与) | TRUE ,如果 $a 和 $b 都为 TRUE 。 |
$a || $b | Or(逻辑或) | TRUE ,如果 $a 或 $b 任一为 TRUE 。 |
“与”和“或”有两种不同形式运算符的原因是它们运算的优先级不同(见运算符优先级)。
Example #1 逻辑运算符示例
<?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);
?>
以上例程的输出类似于:
bool(true) bool(false) bool(false) bool(true)
f dot sylvestris at gmail dot com (2013-04-07 22:11:35)
The lesson I take from this manpage is this: use more parentheses than you think you need. Seriously, before I read this, I thought assignment had the lowest possible precedence. Of course, I always parethesize defensively so that I don't have to bother memorizing operator precedences, but now I'm especially glad I developed that habit.
dartello at gmail dot com (2012-12-12 14:35:44)
Unlike in C/C++ the invertor (Not) in PHP assumes a string:
<?php
$a = 1;
$t = !$a;
var_dump($t);
?>
The above example will output:
string(0) => ""
To approach the C/C++ handling, this can be solved as follows:
<?php
$a = 1;
(int) $t = !$a;
var_dump($t);
(bool) $u = !a;
var_dump($u);
?>
The above example will output:
int(0)
bool(false)
phpnet at zc dot webhop dot net (2012-12-07 15:07:06)
This works similar to javascripts short-curcuit assignments and setting defaults. (e.g. var a = getParm() || 'a default';)
<?php
($a = $_GET['var']) || ($a = 'a default');
?>
$a gets assigned $_GET['var'] if there's anything in it or it will fallback to 'a default'
Parentheses are required, otherwise you'll end up with $a being a boolean.
momrom at freenet dot de (2009-04-19 08:32:43)
Evaluation of logical expressions is stopped as soon as the result is known.
If you don't want this, you can replace the and-operator by min() and the or-operator by max().
<?php
function a($x) { echo 'Expression '; return $x; }
function b($x) { echo 'is '; return $x; }
function c($x) { echo $x ? 'true.' : 'false.' ;}
c( a( false ) and b( true ) ); // Output: Expression false.
c( min( a( false ), b( true ) ) ); // Output: Expression is false.
c( a( true ) or b( true ) ); // Output: Expression true.
c( max( a( true ), b( true ) ) ); // Output: Expression is true.
?>
This way, values aren't automaticaly converted to boolean like it would be done when using and or or. Therefore, if you aren't sure the values are already boolean, you have to convert them 'by hand':
<?php
c( min( (bool) a( false ), (bool) b( true ) ) );
?>
pepesantillan at gmail dot com (2007-12-23 15:23:20)
worth reading for people learning about php and programming: (adding extras <?php ?> to get highlighted code)
about the following example in this page manual:
Example#1 Logical operators illustrated
...
<?php
// "||" has a greater precedence than "or"
$e = false || true; // $e will be assigned to (false || true) which is true
$f = false or true; // $f will be assigned to false
var_dump($e, $f);
// "&&" has a greater precedence than "and"
$g = true && false; // $g will be assigned to (true && false) which is false
$h = true and false; // $h will be assigned to true
var_dump($g, $h);
?>
_______________________________________________end of my quote...
If necessary, I wanted to give further explanation on this and say that when we write:
$f = false or true; // $f will be assigned to false
the explanation:
"||" has a greater precedence than "or"
its true. But a more acurate one would be
"||" has greater precedence than "or" and than "=", whereas "or" doesnt have greater precedence than "=", so
<?php
$f = false or true;
//is like writting
($f = false ) or true;
//and
$e = false || true;
is the same as
$e = (false || true);
?>
same goes for "&&" and "AND".
If you find it hard to remember operators precedence you can always use parenthesys - "(" and ")". And even if you get to learn it remember that being a good programmer is not showing you can do code with fewer words. The point of being a good programmer is writting code that is easy to understand (comment your code when necessary!), easy to maintain and with high efficiency, among other things.
paranoiq at centrum dot cz (2007-11-19 08:00:01)
and, or and xor can be used as conditional constructs:
<?php
// do_that() is executed only if do_this() returns false
if($something) do_this() or do_that();
// $b is assigned to $b, do_that() is executed if $b is false
if($something) $a = $b or do_that();
// do_that() is executed only if do_this() returns true
if($something) do_this() and do_that();
// $b is assigned to $b, do_that() is executed if $b is true
if($something) $a = $b and do_that();
// both do_that() and do_this() are executed..
if($something) do_this() xor do_that();
// .. so the behaviour is same as:
if($something) {
do_this();
do_that();
}
?>
for understanding what happens if $b is NULL or do_this() returns NULL, read the avbentem's comment on NULL type. generaly speaking, NULL is threated like false in most cases.
peter dot kutak at NOSPAM dot gmail dot com (2007-10-01 12:36:00)
$test = true and false; ---> $test === true
$test = (true and false); ---> $test === false
$test = true && false; ---> $test === false
NOTE: this is due to the first line actually being
($test = true) and false;
due to "&&" having a higher precedence than "=" while "and" has a lower one
Lawrence (2007-08-28 12:04:13)
Note that PHP's boolean operators *always* return a boolean value... as opposed to other languages that return the value of the last evaluated expression.
For example:
$a = 0 || 'avacado';
print "A: $a\n";
will print:
A: 1
in PHP -- as opposed to printing "A: avacado" as it would in a language like Perl or JavaScript.
This means you can't use the '||' operator to set a default value:
$a = $fruit || 'apple';
instead, you have to use the '?:' operator:
$a = ($fruit ? $fruit : 'apple');
Andrew (2007-08-13 08:49:48)
> <?php
> your_function() or return "whatever";
> ?>
doesn't work because return is not an expression, it's a statement. if return was a function it'd work fine. :/