您现在的位置是:群英 > 开发技术 > PHP语言
PHP中unset的用法是什么,如何掌握
Admin发表于 2022-05-11 12:01:53816 次浏览
相信很多人对“PHP中unset的用法是什么,如何掌握”都不太了解,下面群英小编为你详细解释一下这个问题,希望对你有一定的帮助


概述

unset()经常会被用到,用于销毁指定的变量,但它有自己的行为模式,如果不仔细的话可能会被中文解释给迷惑:

先来看看官方文档的说法:

unset  ―― unset a given variable

void unset (mixed $var [,mixed $...]); 

parameters:

var:The variable to be unset.    //要unset的变量

...:Another variable ... //其他需要unset的变量

return Values:No value is returned.    //unset不返回值

Because this is a language construct and not a function,it cannot be called using variable functions.

//unset()是语言结构,不是函数,因此不能被函数变量调用,具体参照函数变量。

使用function_exists('unset')返回的false,以此证明unset并不是一个函数,所以无法使用$fun='unset';$fun()的方式调用unset()

It is possible to unset even object properties visible in current context.

//通用环境下unset可以销毁对象或者对象的可见属性(public)

It is not possible to unset $this inside an object method since PHP5

在php5之前unset无法销毁对象中的$this方法

when using unset() on inaccessible  object properties,the __unset() overloading method will be called,if declare.

当unset()无法销毁对象中的属性,例如私有属性,保护属性,那么会自动加载对象中的__unset方法。

description:

unset() destroys the specified variables.     //unset()销毁指定的变量

The behavior of unset() inside of a function can vary dependiing on what type of variable you are attempting to destroy.

//unset()的行为在函数内部可以根据你所指定销毁的变量类型变化。

变化情况

情况一:

if a globalized variable is unset() inside of a function,only the local variable is destroyed.The variable in the calling environment will retain the same value as before unset() was called.

如果在函数内一个使用global使其全局化的变量,使用unset进行销毁,那么只有局部的变量会被销毁,在调用环境的变量将会保留没有使用unset()销毁之前的调用的变量值。

the example:

<?php
function destroy_foo() 
{
    global $foo;
    unset($foo);
}
$foo = 'bar';
destroy_foo();
echo $foo;
?>

the above example will output:bar

这是官方文档的例子,可能这样还是不太明显,把上面的例子改成下面这样,一切就很清晰了。

<?php 
function des(){
	global $foo;
	$foo='bars';
	unset($foo);
	echo $foo;
}
$foo='bar';
echo "The globalized variable is unset() inside of a function:";
des();
echo "<br/>";
echo "The variable in the calling environment:";
echo $foo;

上面的例子会返回如下结果:

可以看到函数内echo $foo会得到错误提示该变量没有定义,因为unset将$foo在函数内的局部变量销毁了。

而外部调用环境的$foo仍保留着没有被unset进行销毁,上面官方描述上写了调用环境的$foo将保留的是在使用unset()前的变量值,因此echo出bars,而不是bar。

to unset() a global variable inside of a function,then use the $GLOBALS array to do so.

如果要用unset()销毁函数中的全局变量,应该使用$GLOBALS数组形式

function destroy_foo(){
	unset($GLOBALS['foo']);
}
$foo = 'bar';
destroy_foo();
echo $foo;

the above example whill output: Notice: Undefined variable: foo in ...

延伸:

这里可以明确一点,函数内global修饰的变量$var会使其全局化,但和$GLOBALS['var']性质不同,虽然他们都是使用的外部调用环境的$var,但是函数内global $var里保存的只是外部环境调用变量$val的一个指针或者同名引用,而$GLOBALS['var']是外部调用环境$var本身,因此unset在函数内对两者进行销毁会产生上面例子的不同的原因,前者销毁的只是保存同名引用或者指针的变量$var,后者销毁的是外部调用变量$var本身。

情况二:

if a variable  that is PASSED BY REFERENCE is unset() inside of a function,only the local variable is destroyed.The variable in the calling environment will retain the same value as before unset() was called.

如果一个被引用的函数在函数内部使用unset()进行销毁,那么只有函数内部的局部变量被销毁,而调用环境变量仍然会保留unset()之前被调用的值。

<?php 
function foo(&$bar){
	unset($bar);
	$bar="blah";
}
$bar='something';
echo $bar."\n";
foo($bar);
echo $bar."\n";

The above example will output:something something

这个和上面global其实很相似,函数内部引用的变量$bar其实保存的是外部环境变量$bar的指针或者说外部变量$bar的引用,因此unset在函数内部销毁的并不是外部环境调用的变量,因此外部环境调用的变量$bar还存在。

情况三:

if a static variable is unset() inside of a function,unset() destroys the variable only in the context of the rest of a function.Following calls will restore the previous value of a variable.

如果是函数内部的静态变量使用unset(),unset()销毁掉函数内部的静态变量,但是下次调用会恢复之前该静态变量的值。
为了更明显,下面的例子进行对比,这里对官方的范例进行了修改对比:

<?php 
function fun1(){
	static $count=0;
	$count++;
	echo "before:".$count." ";
	$count='2';
	echo "after".$count." ";
}
for($i=0;$i<5;$i++){
	fun1();
	echo "<br/>";
}

output: 

下面使用unset:

<?php 
function fun1(){
	static $count=0;
	$count++;
	echo "before:".$count." ";
	unset($count);
	$count='2';
	echo "after".$count." ";
}
for($i=0;$i<5;$i++){
	fun1();
	echo "<br/>";
}

output: 

两个对比可以看出,unset只是将函数内部的静态变量进行了销毁,但没有销毁储存在内存里的该静态变量的值,因为在函数内部用static声明的静态变量存储的仅仅是指向内存存储该静态变量的一个指针,因此unset()销毁的时候也仅仅是销毁了该指针,但存储在内存里的静态变量值没有受到影响,因此再次调用该函数的时候,static该变量再次建立了与内存中该变量的指针关系,而上一次调用时unset()之前的该变量值依旧存在,因此会恢复该变量上一次的值,因此$count进行了递增。


以上就是关于“PHP中unset的用法是什么,如何掌握”的介绍了,感谢各位的阅读,希望文本对大家有所帮助。如果想要了解更多知识,欢迎关注群英网络,小编每天都会为大家更新不同的知识。

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。

标签: php中unset
相关信息推荐
2022-05-31 17:48:08 
摘要:java内存不足的解决办法:1、linux上利用top命令查看所有进程,大内存的选择性的kill,释放内存;2、调整tomca中对jvm内存的配置,如将最小内存设置的更小点。
2022-09-06 17:48:39 
摘要:javascript作为脚本语言, 可以为页面上的元素绑定事件,用于在指定事件发生时能自动调用相应的事件处理程序处理事件。那么怎么给元素添加事件?下面本篇文章给大家介绍一下JS绑定事件三种方式,希望对大家有所帮助!
2022-08-22 17:58:58 
摘要:C语言里可以用#define定义一个标识符来表示一个常量。特点是:定义的标识符不占内存,只是一个临时的符号,预编译后这个符号就不存在了,也不做类型定义。预编译又叫预处理
云活动
推荐内容
热门关键词
热门信息
群英网络助力开启安全的云计算之旅
立即注册,领取新人大礼包
  • 联系我们
  • 24小时售后:4006784567
  • 24小时TEL :0668-2555666
  • 售前咨询TEL:400-678-4567

  • 官方微信

    官方微信
Copyright  ©  QY  Network  Company  Ltd. All  Rights  Reserved. 2003-2019  群英网络  版权所有   茂名市群英网络有限公司
增值电信经营许可证 : B1.B2-20140078   粤ICP备09006778号
免费拨打  400-678-4567
免费拨打  400-678-4567 免费拨打 400-678-4567 或 0668-2555555
微信公众号
返回顶部
返回顶部 返回顶部