php && 邏輯與運算符使用說明
更新時間:2010年03月04日 19:02:25 作者:
如果前面的判斷為假后面的則不執(zhí)行,如果是真,繼續(xù)執(zhí)行下面的操作。
例子:
!defined('MAGIC_QUOTES_GPC') && define('MAGIC_QUOTES_GPC', get_magic_quotes_gpc());
o(︶︿︶)o 唉,很暈,今天問了N多的人。終于把“&&”東西給弄明白怎么回事了
運算符都沒有判斷就那樣寫什么意思,哎,原來如果前面的為假。后面的語句就不執(zhí)行了。免得我們還費勁的寫if
這樣多簡單。。。
//簡單說明,如果前面的判斷為假后面的則不執(zhí)行,如果是真,繼續(xù)執(zhí)行下面的定義常量操作。
Example #1 邏輯運算符示例
<?php
// 下面的 foo() 不會被調(diào)用,因為它們被運算符“短路”了。
$a = (false && foo());
$b = (true || foo());
$c = (false and foo());
$d = (true or foo());
// "||" 的優(yōu)先級比 "or" 高
$e = false || true; // $e 被賦值為 (false || true),結(jié)果為 true
$f = false or true; // $f 被賦值為 false [Altair注:"=" 的優(yōu)先級比 "or" 高]
var_dump($e, $f);
// "&&" 的優(yōu)先級比 "and" 高
$g = true && false; // $g 被賦值為 (true && false),結(jié)果為 false
$h = true and false; // $h 被賦值為 true [Altair注:"=" 的優(yōu)先級比 "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.
!defined('MAGIC_QUOTES_GPC') && define('MAGIC_QUOTES_GPC', get_magic_quotes_gpc());
o(︶︿︶)o 唉,很暈,今天問了N多的人。終于把“&&”東西給弄明白怎么回事了
運算符都沒有判斷就那樣寫什么意思,哎,原來如果前面的為假。后面的語句就不執(zhí)行了。免得我們還費勁的寫if
這樣多簡單。。。
//簡單說明,如果前面的判斷為假后面的則不執(zhí)行,如果是真,繼續(xù)執(zhí)行下面的定義常量操作。
例子 | 名稱 | 結(jié)果 |
---|---|---|
$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 邏輯運算符示例
復(fù)制代碼 代碼如下:
<?php
// 下面的 foo() 不會被調(diào)用,因為它們被運算符“短路”了。
$a = (false && foo());
$b = (true || foo());
$c = (false and foo());
$d = (true or foo());
// "||" 的優(yōu)先級比 "or" 高
$e = false || true; // $e 被賦值為 (false || true),結(jié)果為 true
$f = false or true; // $f 被賦值為 false [Altair注:"=" 的優(yōu)先級比 "or" 高]
var_dump($e, $f);
// "&&" 的優(yōu)先級比 "and" 高
$g = true && false; // $g 被賦值為 (true && false),結(jié)果為 false
$h = true and false; // $h 被賦值為 true [Altair注:"=" 的優(yōu)先級比 "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.
相關(guān)文章
解析php中用PHPMailer來發(fā)送郵件的示例(126.com的例子)
本篇文章是對php中用PHPMailer來發(fā)送郵件的示例(126.com的例子)進行了詳細的分析介紹,需要的朋友參考下2013-06-06PHP 出現(xiàn)亂碼和Sessions驗證問題的解決方法!
PHP程序語言編碼一定要統(tǒng)一為UTF-8或GB2312如果選擇其他的語言在里面中文會出現(xiàn)亂碼的。2008-12-12淺析PHP substr,mb_substr以及mb_strcut的區(qū)別和用法
本篇文章是對PHP中的substr,mb_substr以及mb_strcut區(qū)別和用法進行了詳細的分析介紹,需要的朋友參考下2013-06-06php使用fputcsv()函數(shù)csv文件讀寫數(shù)據(jù)的方法
這篇文章主要介紹了php使用fputcsv()函數(shù)csv文件讀寫數(shù)據(jù)的方法,分析了fputcsv()函數(shù)針對csv文件的讀寫操作技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-01-01PHP實現(xiàn)的鏈式隊列結(jié)構(gòu)示例
這篇文章主要介紹了PHP實現(xiàn)的鏈式隊列結(jié)構(gòu),結(jié)合具體實例形式分析了php鏈式隊列的定義及入隊、出隊、打印隊列等基本操作實現(xiàn)與使用方法,需要的朋友可以參考下2017-09-09JavaScript+PHP實現(xiàn)視頻文件分片上傳的示例代碼
這篇文章主要介紹了基于JavaScript+PHP實現(xiàn)視頻文件分片上傳,視頻文件分片上傳,整體思路是利用JavaScript將文件切片,然后循環(huán)調(diào)用上傳接口 upload.php 將切片上傳到服務(wù)器,文中有詳細代碼供大家參考,需要的朋友可以參考下2024-02-02