搜索
首页后端开发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字符串有没有下标php字符串有没有下标Apr 24, 2022 am 11:49 AM

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

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怎么读取字符串后几个字符php怎么读取字符串后几个字符Apr 22, 2022 pm 08:31 PM

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

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尊渡假赌尊渡假赌尊渡假赌
仓库:如何复兴队友
1 个月前By尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
4 周前By尊渡假赌尊渡假赌尊渡假赌

热工具

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )专业的PHP集成开发工具

安全考试浏览器

安全考试浏览器

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

SublimeText3 英文版

SublimeText3 英文版

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

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)