運(yùn)算符都沒有判斷就那樣寫什么意思,哎絮识,原來如果前面的為假驶乾。后面的語句就不執(zhí)行了。免得我們還費(fèi)勁的寫if
這樣多簡單喊暖。。撕瞧。
//簡單說明陵叽,如果前面的判斷為假后面的則不執(zhí)行,如果是真丛版,繼續(xù)執(zhí)行下面的定義常量操作巩掺。
邏輯運(yùn)算符
例子名稱結(jié)果
$a and $bAnd(邏輯與)TRUE,如果 $a 與 $b 都為TRUE页畦。
$a or $bOr(邏輯或)TRUE胖替,如果 $a 或 $b 任一為TRUE。
$a xor $bXor(邏輯異或)TRUE寇漫,如果 $a 或 $b 任一為TRUE刊殉,但不同時(shí)是。
! $aNot(邏輯非)TRUE州胳,如果 $a 不為TRUE记焊。
$a && $bAnd(邏輯與)TRUE,如果 $a 與 $b 都為TRUE栓撞。
$a || $bOr(邏輯或)TRUE遍膜,如果 $a 或 $b 任一為TRUE。
Example #1 邏輯運(yùn)算符示例
代碼如下:
// 下面的 foo() 不會(huì)被調(diào)用瓤湘,因?yàn)樗鼈儽贿\(yùn)算符“短路”了瓢颅。
$a = (false && foo());
$b = (true || foo());
$c = (false and foo());
$d = (true or foo());
// "||" 的優(yōu)先級(jí)比 "or" 高
$e = false || true; // $e 被賦值為 (false || true),結(jié)果為 true
$f = false or true; // $f 被賦值為 false [Altair注:"=" 的優(yōu)先級(jí)比 "or" 高]
var_dump($e, $f);
// "&&" 的優(yōu)先級(jí)比 "and" 高
$g = true && false; // $g 被賦值為 (true && false)弛说,結(jié)果為 false
$h = true and false; // $h 被賦值為 true [Altair注:"=" 的優(yōu)先級(jí)比 "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.