首页  >  文章  >  后端开发  >  PHP 取消设置()

PHP 取消设置()

王林
王林原创
2024-08-29 12:53:09983浏览

以下文章提供了有关 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 MD5()下一篇:PHP crypt()