PHP 中“isset()”和“!empty()”有什麼區別?


isset 函式

ISSET 檢查變數以檢視是否已設定。換言之,它檢查變數是否為 NULL 以外的任何值或未賦值。如果變數存在並具有非 NULL 值,則 ISSET 返回 TRUE。這意味著分配給 “”、“0”、“0”或 FALSE 的變數會設定,因此對於 ISSET 它們是 TRUE。

示例

 線上演示

<?php
   $val = '0';
   if( isset($val)) {
      print_r(" $val is set with isset function <br>");
   }
   $my_array = array();
   echo isset($my_array['New_value']) ?
   'array is set.' :  'array is not set.';
?>

輸出

這將產生以下輸出 −

0 is set with isset function
array is not set.

!empty 函式

EMPTY 檢查變數是否為空。空被解釋為:""(空字串)、0(整數)、0.0(浮點數)、“0”(字串)、NULL、FALSE、array()(空陣列)和 “$var;” (在類中宣告的變數,但沒有值)。

示例

 線上演示

<?php
   $temp_val = 0;
   if (empty($temp_val)) {
      echo $temp_val . ' is considered empty';
   }
   echo "nn";
   $new_val = 1;
   if (!empty($new_val)) {
      echo $new_val . ' is considered set';
   }
?>

輸出

這將產生以下輸出 −

0 is considered empty 1 is considered set

更新於: 07-Apr-2020

815 瀏覽

啟動你的 職業

完成課程後獲得認證

開始
廣告
© . All rights reserved.