PHP - isset() 函式



定義和用法

isset() 函式用於確定變數是否已設定/宣告且不為 null。

如果使用 unset() 函式取消設定了變數,則不再認為該變數已設定。

語法

bool isset ( mixed $var , mixed ...$vars )

引數

序號 引數 & 描述
1

var

必填。要檢查的變數。

2

vars

可選。其他變數。

返回值

如果 var 存在且具有任何非 null 值,則此函式返回 true。否則返回 false

如果檢查的變數已賦值為 null,則此函式將返回 false。空字元 ("\0") 不等同於 PHP 的 null 常量。

如果提供了多個引數,則只有當所有引數都被認為已設定時,isset() 才返回 true。評估過程從左到右進行,一旦遇到未設定的變數就停止。

依賴

PHP 4 及以上版本

示例

以下示例演示了 isset() 函式的用法:

<?php
   $a = "";
   echo "a is ".( isset($a)? 'set' : 'not set') . "<br>";

   $b = "test";
   echo "b is ".( isset($b)? 'set' : 'not set') . "<br>";

   $c = "test2";
   echo "checking b and c are ".( isset($b,$c)? 'set' : 'not set') . "<br>";

   echo "****after unset b**** <br>";
   unset ($b);
   echo "b is ".( isset($b)? 'set' : 'not set') . "<br>";
   echo "checking b and c are ".( isset($b,$c)? 'set' : 'not set') . "<br>";

   $d = 1;
   echo "d is ".( isset($d)? 'set' : 'not set') . "<br>";

   $e = NULL;
   echo "e is ".( isset($e)? 'set' : 'not set') . "<br>";

   $f = array('a' => 'apple');
   echo "f is ".( isset($f)? 'set' : 'not set') . "<br>";

   $g = new stdClass();
   echo "g is ".( isset($g)? 'set' : 'not set') . "<br>";

   $h = '';
   echo "h is ".( isset($h)? 'set' : 'not set') . "<br>";
?>

輸出

將產生以下結果:

a is set
b is set
checking b and c are set
****after unset b****
b is not set
checking b and c are not set
d is set
e is not set
f is set
g is set
h is set
php_variable_handling_functions.htm
廣告