PHP - unset() 函式



定義和用法

unset() 函式銷燬指定的變數。unset() 在函式內部的行為可能因嘗試銷燬的變數型別而異。

  • 如果在函式內部 unset() 了一個全域性變數,則只會銷燬區域性變數。

  • 呼叫環境中的變數將保留與呼叫 unset() 之前相同的值。

語法

void unset ( mixed $var , mixed $vars )

引數

序號 引數 & 描述
1

var

必填。要取消設定的變數。

2

vars

可選。其他變數。

返回值

此函式不返回值。

依賴

PHP 4 及以上版本

示例

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

<?php
   $a = "Welcome TutorialsPoint!";
   echo "The value of 'a' before unset: " . $a . "<br>";
   unset($a);
   echo "The value of 'a' after unset: " . $a;
?>

輸出

這將產生以下結果:

The value of 'a' before unset: Welcome TutorialsPoint!
The value of 'a' after unset:

示例

以下示例演示了在函式內部 unset() 全域性變數的情況:

<?php
   //globalized variable is unset() inside of a function
   function destroy_a() {
      global $a;
      unset($a);
   }

   $a = 'Welcome TutorialsPoint!';
   destroy_a();
   echo "global unset inside function =".$a;
?>

輸出

這將產生以下結果:

global unset inside function =Welcome TutorialsPoint!

示例

如果按引用傳遞的變數在函式內部被 unset(),則只會銷燬區域性變數。呼叫環境中的變數將保留與呼叫 unset() 之前相同的值。以下示例演示了這一點:

<?php
   function destroy_c($v) {
      unset($c);
      $c = "heloo";
   }
   $c = 'TutorialsPoint';
   echo "$c<br>";

   destroy_c($c);
   echo "$c<br>";
?>

輸出

這將不會產生結果:

TutorialsPoint
TutorialsPoint
php_variable_handling_functions.htm
廣告