以下文章提供了有關 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() 方法時被銷毀。
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」的本機版本被破壞,而全域版本保持不變。
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 值。
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() 方法僅重置本地作用域中變數的內容,而不影響外部作用域的內容。
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() 方法後執行的命令中重設。
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]." "; ?>
輸出:
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 取消設定()的詳細內容。更多資訊請關注PHP中文網其他相關文章!

在PHP中,trait適用於需要方法復用但不適合使用繼承的情況。 1)trait允許在類中復用方法,避免多重繼承複雜性。 2)使用trait時需注意方法衝突,可通過insteadof和as關鍵字解決。 3)應避免過度使用trait,保持其單一職責,以優化性能和提高代碼可維護性。

依賴注入容器(DIC)是一種管理和提供對象依賴關係的工具,用於PHP項目中。 DIC的主要好處包括:1.解耦,使組件獨立,代碼易維護和測試;2.靈活性,易替換或修改依賴關係;3.可測試性,方便注入mock對象進行單元測試。

SplFixedArray在PHP中是一種固定大小的數組,適用於需要高性能和低內存使用量的場景。 1)它在創建時需指定大小,避免動態調整帶來的開銷。 2)基於C語言數組,直接操作內存,訪問速度快。 3)適合大規模數據處理和內存敏感環境,但需謹慎使用,因其大小固定。

PHP通過$\_FILES變量處理文件上傳,確保安全性的方法包括:1.檢查上傳錯誤,2.驗證文件類型和大小,3.防止文件覆蓋,4.移動文件到永久存儲位置。

JavaScript中處理空值可以使用NullCoalescingOperator(??)和NullCoalescingAssignmentOperator(??=)。 1.??返回第一個非null或非undefined的操作數。 2.??=將變量賦值為右操作數的值,但前提是該變量為null或undefined。這些操作符簡化了代碼邏輯,提高了可讀性和性能。

CSP重要因為它能防範XSS攻擊和限制資源加載,提升網站安全性。 1.CSP是HTTP響應頭的一部分,通過嚴格策略限制惡意行為。 2.基本用法是只允許從同源加載資源。 3.高級用法可設置更細粒度的策略,如允許特定域名加載腳本和样式。 4.使用Content-Security-Policy-Report-Only頭部可調試和優化CSP策略。

HTTP請求方法包括GET、POST、PUT和DELETE,分別用於獲取、提交、更新和刪除資源。 1.GET方法用於獲取資源,適用於讀取操作。 2.POST方法用於提交數據,常用於創建新資源。 3.PUT方法用於更新資源,適用於完整更新。 4.DELETE方法用於刪除資源,適用於刪除操作。

HTTPS是一種在HTTP基礎上增加安全層的協議,主要通過加密數據保護用戶隱私和數據安全。其工作原理包括TLS握手、證書驗證和加密通信。實現HTTPS時需注意證書管理、性能影響和混合內容問題。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

Dreamweaver CS6
視覺化網頁開發工具

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

禪工作室 13.0.1
強大的PHP整合開發環境

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境