以下文章提供了有关 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(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

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

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

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

PhpStorm Mac 版本
最新(2018.2.1 )专业的PHP集成开发工具

安全考试浏览器
Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。

SublimeText3 英文版
推荐:为Win版本,支持代码提示!

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)