php 邏輯與運算子使用說明

來源:互聯網
上載者:User

例子:
!defined('MAGIC_QUOTES_GPC') && define('MAGIC_QUOTES_GPC', get_magic_quotes_gpc());

o(︶︿︶)o 唉,很暈,今天問了N多的人。終於把“&&”東西給弄明白怎麼回事了

運算子都沒有判斷就那樣寫什麼意思,哎,原來如果前面的為假。後面的語句就不執行了。免得我們還費勁的寫if

這樣多簡單。。。

//簡單說明,如果前面的判斷為假後面的則不執行,如果是真,繼續執行下面的定義常量操作。

邏輯運算子
例子 名稱 結果
$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" 高
$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.

<?php
(isset($panelemail) && !empty($panelemail) ? $panelemail : $userdata['email']);
?>
returns the userdata email address, but this

<?php
(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:

<?php
true && false || false
?>
returns false, but

<?php
true AND false || false
?>
returns true.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.