判斷php變數是否定義,是否為空白
isset() 【1】
Returns?TRUE?if?var?exists and has value other than?NULL,?FALSE?otherwise.
輸入可以是多個變數,只有所有的變數為真的時候,返回真
empty()【2】
Returns?FALSE?if?var?has a non-empty and non-zero value.
The following things are considered to be empty:
- "" (an empty string)
- 0 (0 as an integer)
- "0" (0 as a string)
- NULL
- FALSE
- array() (an empty array)
- var $var; (a variable declared, but without a value in a class)
輸入只能是一個變數
is_null() 【3】
Returns?TRUE?if?var?is?null?,?FALSE?otherwise.
?
?
?? A variable is considered to be?null?if:
?
?
$x
使用 PHP 函數對變數??進行比較
運算式gettype()empty()is_null()isset()if($x) Boolean
| $x = ""; |
string |
TRUE |
FALSE |
TRUE |
FALSE |
| $x = null; |
NULL |
TRUE |
TRUE |
FALSE |
FALSE |
| var $x; |
NULL |
TRUE |
TRUE |
FALSE |
FALSE |
| $x?is undefined |
NULL |
TRUE |
TRUE |
FALSE |
FALSE |
| $x = array(); |
array |
TRUE |
FALSE |
TRUE |
FALSE |
| $x = false; |
boolean |
TRUE |
FALSE |
TRUE |
FALSE |
| $x = true; |
boolean |
FALSE |
FALSE |
TRUE |
TRUE |
| $x = 1; |
integer |
FALSE |
FALSE |
TRUE |
TRUE |
| $x = 42; |
integer |
FALSE |
FALSE |
TRUE |
TRUE |
| $x = 0; |
integer |
TRUE |
FALSE |
TRUE |
FALSE |
| $x = -1; |
integer |
FALSE |
FALSE |
TRUE |
TRUE |
| $x = "1"; |
string |
FALSE |
FALSE |
TRUE |
TRUE |
| $x = "0"; |
string |
TRUE |
FALSE |
TRUE |
FALSE |
| $x = "-1"; |
string |
FALSE |
FALSE |
TRUE |
TRUE |
| $x = "php"; |
string |
FALSE |
FALSE |
TRUE |
TRUE |
| $x = "true"; |
string |
FALSE |
FALSE |
TRUE |
TRUE |
| $x = "false"; |
string |
FALSE |
FALSE |
TRUE |
TRUE |
?
(上表沒有找到原始來源,誰知道請告訴我)
如果變數是一個object該如何呢?
?
運算式gettype()empty()is_null()isset()if($x) Boolean
| $x = new object()? |
object |
FALSE |
FALSE |
TRUE |
TRUE |
?
參考:
【1】http://php.net/manual/en/function.isset.php
【2】http://www.php.net/manual/en/function.empty.php
【3】http://www.php.net/manual/en/function.is-null.php
?
?
http://blog.csdn.net/autofei/archive/2010/05/24/5619004.aspx