PHP 取消設定()

王林
發布: 2024-08-29 12:53:09
原創
981 人瀏覽過

以下文章提供了有關 PHP unset() 的概述。 unset() 方法的主要操作是銷毀指定為其輸入參數的變數。換句話說,它對所選變數執行重置操作。但是,其行為可能會根據要銷毀的變數的類型而有所不同。 PHP4以上版本支援此功能。

開始您的免費軟體開發課程

網頁開發、程式語言、軟體測試及其他

PHP unset() 語法

unset(mixed $selectedvar, mixed $selectedvar1,….., mixed $selectedvarN): void
登入後複製
  • selectedvar: unset() 方法的強制參數。至少需要一個要取消設定的變數作為該方法的輸入參數。
  • selectedvarN: 可選參數,可以作為輸入參數提供給 unset() 方法來重置它。

unset() 的用例

以下是不同的情況:

1.對局部變數應用 unset()

當局部變數傳遞給 unset 函數時,函數會重設該變數。

範例:

代碼:

<?php
$input = "I have value!!!";
echo "The value of 'input' before unset: " . $input . "<br>";
unset($input); //Applying unset() method on $input variable
echo "The value of 'input' after unset: " . $input;
?>
登入後複製

輸出:

變數「input」中包含的值在執行 unset() 方法時被銷毀。

PHP 取消設定()

2.對全域變數函數內的變數應用 unset

當使用者嘗試對函數內的變數使用 Unset 且該變數也定義為全域變數時,則 unset() 僅重設本機變數。全球範圍不受影響。

範例

代碼:

<?php
function Afunction()
{
$Avariable = 'local value';
echo "Within the function scope before unset: ".$Avariable."<br>";
global $Avariable;
unset($Avariable); //Deletes the local ‘Avariable’
echo "Within the function scope after unset:  ".$Avariable."<br>";
}
$Avariable = 'Global Value'; //The global ‘Avariable’
echo "Out of the function scope before unset:  ".$Avariable."<br>";
Afunction();
echo "Out of the function scope after unset: ".$Avariable."<br>";
?>
登入後複製

輸出:

變數「Avariable」的本機版本被破壞,而全域版本保持不變。

PHP 取消設定()

3.對函數內的全域變數應用 unset

如果函數內的變數也宣告為全域變量,而使用者需要銷毀全域變量,則可以使用數組[$GLOBAL]來實作。

範例

代碼:

<?php
function Afunction()
{
$Avariable = 'local value';
echo "Within the function scope before unset: ".$Avariable."<br>";
global $Avariable;
unset($GLOBALS['Avariable']); //Resets the global ‘Avariable’
echo "Within the function scope after unset:  ".$Avariable."<br>";
}
$Avariable = 'Global Value';
echo "Out of the function scope before unset:  ".$Avariable."<br>";
Afunction();
echo "Out of the function scope after unset: ".$Avariable."<br>";
?>
登入後複製

輸出:

變數「Avariable」的本機版本不受執行 unset 函數的影響,而變數的全域版本則設定為 null 值。

PHP 取消設定()

4.應用 unset() 來傳遞引用變數

如果對作為參考傳遞給函數的變數呼叫 unset(),則 unset() 僅重設本機變數。呼叫環境中的變數實例保持原樣。

範例

代碼:

<?php
function Afunction(&$Avariable) //’Avariable’ is the pass by reference
{
$Avariable = 'Internal value';
echo "Within the function scope before unset: ".$Avariable."<br>";
unset($Avariable); //Resets the local ‘Avariable’
echo "Within the function scope after unset:  ".$Avariable."<br>";
}
$Avariable = 'External Value';
echo "Out of the function scope before unset:  ".$Avariable."<br>";
Afunction($Avariable);
echo "Out of the function scope after unset: ".$Avariable."<br>";
?>
登入後複製

輸出:

透過引用變數「Avariable」傳遞時呼叫的 unset() 方法僅重置本地作用域中變數的內容,而不影響外部作用域的內容。

PHP 取消設定()

5.對靜態變數應用 unset()

當靜態變數設定為 unset() 方法的輸入參數時,在呼叫函數 unset() 後,該變數將針對函數作用域中的剩餘命令重設。

範例

代碼:

<?php
function UnsetStatic()
{
static $staticvar;
$staticvar++;
echo "Before unset() method is called: $staticvar"."<br>";
//Deletes ‘staticvar’ only for the below commands within the scope of this ‘UnsetStatic’ function
unset($staticvar);
echo "after unset() method is called: $staticvar"."<br>";
}
UnsetStatic();
UnsetStatic();
UnsetStatic();
?>
登入後複製

輸出:

變數「staticvar」僅在呼叫 unset() 方法後執行的命令中重設。

PHP 取消設定()

6.對陣列元素套用 unset()

對陣列元素套用 unset() 方法會從陣列中刪除該元素,而不執行重新索引操作。

範例

代碼:

<?php
$arrayinput = [0 => "first", 1 => "second", 2 => "third"];
Echo "The array elements, before unset:"."<br>";
Echo $arrayinput[0]."  ". $arrayinput[1]."  ". $arrayinput[2]."  "."<br>";
//Unset operation is called on the second element of the array ‘arrayinput’
unset($arrayinput[1]);
Echo "The array elements, after unset:"."<br>";
Echo $arrayinput[0]."  ". $arrayinput[1]."  ". $arrayinput[2]."  ";
?>
登入後複製

輸出:

PHP 取消設定()

7.一次對多個元素套用 unset()

unset()方法支援一次刪除多個變數。

範例

代碼:

<?php
$input1 = "I am value1";
$input2 = "I am value2";
$input3 = "I am value3";
echo "The value of 'input1' before unset:  " . $input1 . "<br>";
echo "The value of 'input2' before unset:  " . $input2 . "<br>";
echo "The value of 'input3' before unset:  " . $input3 . "<br>";
echo "<br>";
//Reseting input1, input2 and input3 together in single command
unset($input1,$input2,$input3);
echo "The value of 'input1' after unset:  " . $input1."<br>";
echo "The value of 'input2' after unset:  " . $input2."<br>";
echo "The value of 'input3' after unset:  " . $input3."<br>";
?>
登入後複製

輸出:

PHP 取消設定()

注意:(unset) 轉換與函數 unset() 不同。 (unset) 強制轉換僅用作 NULL 類型的強制轉換,而 unset() 方法會更改變數。 unset() 是一種語言構造,因此變數函數不支援。 unset() 方法可用於重置目前範圍內可見的物件屬性,但任何物件方法中的「$this」變數除外。為了對目前作用域中不可存取的物件屬性執行unset操作,需要宣告並呼叫一個重載方法__unset()。

以上是PHP 取消設定()的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
php
來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!