isset()函數是PHP中的內置函數,它檢查變量是否已設置且不為NULL。此函數還檢查聲明的變量,數組或數組鍵是否具有空值,如果是,isset()返回false,它在所有其他可能的情況下返回true。
語法:
bool isset( $var, mixed )
參數:此函數接受多個參數。這個函數的第一個參數是$ var。此參數用于存儲變量的值。
例:
- $num = '0';
- if( isset( $num ) ) {
- print_r(" $num is set with isset function
");- }
- // 聲明一個空數組
- $array = array();
- //Vevb.com
- echo isset($array['geeks']) ?
- 'array is set.' : 'array is not set.';
- ?>
輸出:
0 is set with isset function
array is not set.
empty()函數是一種語言構造,用于確定給定變量是空還是NULL。!empty()函數是empty()函數的否定或補充。empty()函數與!isset()函數相當,而!empty()函數等于isset()函數。
例:
- $temp = 0;
- if (emptyempty($temp)) {
- echo $temp . ' is considered empty';
- }
- echo "/n";
- $new = 1;
- if (!emptyempty($new)) {
- echo $new . ' is considered set';
- }
- ?>
輸出:
0 is considered empty
1 is considered set
檢查兩個函數的原因:
isset()和!empty()函數類似,兩者都將返回相同的結果。但唯一的區別是!當變量不存在時,empty()函數不會生成任何警告或電子通知。它足以使用任何一個功能。通過將兩個功能合并到程序中會導致時間流逝和不必要的內存使用。
例:
- $num = '0';
- if( isset ( $num ) ) {
- print_r( $num . " is set with isset function");
- }
- echo "/n";
- $num = 1;
- if( !emptyempty ( $num ) ) {
- print_r($num . " is set with !empty function");
- }
輸出:
0 is set with isset function
1 is set with !empty function
本篇文章就是為什么要在PHP中同時檢查isset()和!empty()函數的原因介紹,希望對需要的朋友有所幫助!
|
新聞熱點
疑難解答