搜尋
首頁後端開發php教程PHP 取消設定()
PHP 取消設定()Aug 29, 2024 pm 12:53 PM
php

以下文章提供了有關 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中文網其他相關文章!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

php怎么实现几秒后执行一个函数php怎么实现几秒后执行一个函数Apr 24, 2022 pm 01:12 PM

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

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

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

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

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

php怎么判断有没有小数点php怎么判断有没有小数点Apr 20, 2022 pm 08:12 PM

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

php怎么替换nbsp空格符php怎么替换nbsp空格符Apr 24, 2022 pm 02:55 PM

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

php怎么设置implode没有分隔符php怎么设置implode没有分隔符Apr 18, 2022 pm 05:39 PM

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

php字符串有没有下标php字符串有没有下标Apr 24, 2022 am 11:49 AM

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

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

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

AI Clothes Remover

AI Clothes Remover

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

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
2 週前By尊渡假赌尊渡假赌尊渡假赌
倉庫:如何復興隊友
4 週前By尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具