-
unset
鎖定
unset是一個計算機語言,刪除變量或函數PHP語言構造器— 釋放給定的變量。unset()銷燬指定的變量。
- 外文名
- unset
- 補充説明
- PHP-unset()示例:
- 語 法
- unset [-fv][變量或函數名稱];
- 函 數
- 刪除變量或函數
unset功能説明
刪除變量或函數
PHP語言構造器— 釋放給定的變量
unset語法
- unset [-fv][變量或函數名稱];
unset() 銷燬指定的變量。注意在 PHP 3 中,unset() 將返回 TRUE(實際上是整型值 1),而在 PHP 4 中,unset() 不再是一個真正的函數:它現在是一個語句。這樣就沒有了返回值,試圖獲取 unset() 的返回值將導致解析錯誤。
unset補充説明
1、linux系統中unset為shell內建指令,可刪除全局變量;
2、PHP-unset()示例:
(1)
<?php // 銷燬單個變量 unset ($foo); // 銷燬單個數組元素 unset ($bar['quux']); // 銷燬一個以上的變量 unset ($foo1, $foo2, $foo3); ?>
(2)unset() 在函數中的行為會依賴於想要銷燬的變量的類型而有所不同。
如果在函數中 unset() 一個全局變量,則只是局部變量被銷燬,而在調用環境中的變量將保持調用 unset() 之前一樣的值。
<?php function destroy_foo() { global $foo; unset($foo); } $foo = 'bar'; destroy_foo(); echo $foo; ?>
上邊的例子將輸出:bar(3)如果在函數中 unset() 一個通過引用傳遞的變量,則只是局部變量被銷燬,而在調用環境中的變量將保持調用 unset() 之前一樣的值。
<?php function foo(&$bar) { unset($bar); $bar = "blah"; } $bar = 'something'; echo "$bar\n"; foo($bar); echo "$bar\n"; ?>
上邊的例子將輸出:something something
(4)如果在函數中 unset() 一個靜態變量,則 unset() 將銷燬此變量及其所有的引用。
<?php function foo() { static $a; $a++; echo "$a\n"; unset($a); } foo(); foo(); foo(); ?>
上邊的例子將輸出:123
(5)如果您想在函數中 unset() 一個全局變量,可使用 $GLOBALS 數組來實現:
<?php function foo() { unset($GLOBALS['bar']); } $bar = "something"; foo(); ?>
unset參數
-f 僅刪除函數。
-v 僅刪除變量。
- 參考資料
-
- 1. unset .PHP Manual[引用日期2012-09-07]