代码之间有什么区别

时间:2013-11-25 13:55:38

标签: php operators

我在PHP中有以下几行代码:

$a = $b && $c || $d;

$a = $b AND $c || $d;

$a = $b && $c OR $d;

$a = $b AND $c OR $d;

每一行都使用不同的运算符,因此它是不同的,如果是,那有什么区别? 它是如何执行的?

3 个答案:

答案 0 :(得分:3)

执行这些运算符的顺序不同。

Operator Precedence

// The result of the expression (true && false) is assigned to $g
// Acts like: ($g = (true && false))
$g = true && false;

// The constant true is assigned to $h and then false is ignored
// Acts like: (($h = true) and false)
$h = true and false;

答案 1 :(得分:0)

以下是运营商优先顺序:

enter image description here

了解更多信息:http://us2.php.net/manual/en/language.operators.precedence.php

答案 2 :(得分:0)

作为short-circuit, they are the same.&& means, || means或`,它们的行为相同:

// foo() will never get called as those operators are short-circuit
$a = (false && foo());
$b = (true  || foo());
$c = (false and foo());
$d = (true  or  foo());

另一方面,||的优先级高于or

// The result of the expression (false || true) is assigned to $e
// Acts like: ($e = (false || true))
$e = false || true;

// The constant false is assigned to $f and then true is ignored
// Acts like: (($f = false) or true)
$f = false or true;

&&and相同:

// The result of the expression (true && false) is assigned to $g
// Acts like: ($g = (true && false))
$g = true && false;

// The constant true is assigned to $h and then false is ignored
// Acts like: (($h = true) and false)
$h = true and false;
相关问题