運算符都沒有判斷就那樣寫什么意思,哎,原來如果前面的為假。后面的語句就不執行了。免得我們還費勁的寫if
這樣多簡單。。。
//簡單說明,如果前面的判斷為假后面的則不執行,如果是真,繼續執行下面的定義常量操作。
邏輯運算符
例子名稱結果
$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。
Example #1 邏輯運算符示例
代碼如下:
// 下面的 foo() 不會被調用,因為它們被運算符“短路”了。
$a = (false && foo());
$b = (true || foo());
$c = (false and foo());
$d = (true or foo());
// "||" 的優先級比 "or" 高
$e = false || true; // $e 被賦值為 (false || true),結果為 true
$f = false or true; // $f 被賦值為 false [Altair注:"=" 的優先級比 "or" 高]
var_dump($e, $f);
// "&&" 的優先級比 "and" 高
$g = true && false; // $g 被賦值為 (true && false),結果為 false
$h = true and false; // $h 被賦值為 true [Altair注:"=" 的優先級比 "and" 高]
var_dump($g, $h);
?>
上例的輸出類似于:
bool(true)
bool(false)
bool(false)
bool(true)
Another example that might help.
(isset($panelemail) && !empty($panelemail) ? $panelemail : $userdata['email']);
?>
returns the userdata email address, but this
(isset($panelemail) AND !empty($panelemail) ? $panelemail : $userdata['email']);
?>
returns false.
The reason is that the two types of ands have a different order of precedence. "&&" is higher than "AND", and the "?:" operator just happens to come between the two. Also, since "||" (or) is actually higher than "AND," you should never mix &&s and ||s with ANDs and ORs without paretheses.
For example:
true && false || false
?>
returns false, but
true AND false || false
?>
returns true.