以下文章提供了有關 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把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

在PHP中,可以利用implode()函数的第一个参数来设置没有分隔符,该函数的第一个参数用于规定数组元素之间放置的内容,默认是空字符串,也可将第一个参数设置为空,语法为“implode(数组)”或者“implode("",数组)”。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。


熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

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

SublimeText3漢化版
中文版,非常好用

SublimeText3 Linux新版
SublimeText3 Linux最新版

記事本++7.3.1
好用且免費的程式碼編輯器

Dreamweaver CS6
視覺化網頁開發工具