深入理解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()的详细内容,更多关于php unset()的资料请关注我们其它相关文章!

(0)

相关推荐

  • php中用unset销毁变量并释放内存

    PHP的unset()函数用来清除.销毁变量,不用的变量,我们可以用unset()将它销毁.但是某些时候,用unset()却无法达到销毁变量占用的内存! 我们先看一个例子: <?php $s = str_repeat('1',255); //产生由255个1组成的字符串 $m = memory_get_usage(); //获取当前占用内存 unset($s); $mm = memory_get_usage(); //unset()后再查看当前占用内存 echo $m-$mm; ?> 最后输出

  • php使用unset()删除数组中某个单元(键)的方法

    本文实例讲述了php使用unset()删除数组中某个单元(键)的方法.分享给大家供大家参考.具体分析如下: unset既可以删除变量,也可以删除数组中某个单元.但要注意的是,数组不会重建索引. 实例如下: <?php $arr = array("朝阳区","海淀区","西城区","东城区","丰台区"); unset($arr[3]); echo "<pre>"; pr

  • PHP中isset()和unset()函数的用法小结

    isset(PHP 3, PHP 4, PHP 5 ) isset -- 检测变量是否设置 描述bool isset ( mixed var [, mixed var [, ...]])如果 var 存在则返回 TRUE,否则返回 FALSE. 如果已经使用 unset() 释放了一个变量之后,它将不再是 isset().若使用 isset() 测试一个被设置成 NULL 的变量,将返回 FALSE.同时要注意的是一个 NULL 字节("\0")并不等同于 PHP 的 NULL 常数.

  • PHP魔术方法__ISSET、__UNSET使用实例

    __isset()   – 在对类中属性或者非类中属性使用isset()方法的时候如果没有或者非公有属性,则自动执行__isset()的方法 __unset() - 在对类中属性或者非类中属性使用unset()方法的时候如果没有或者非公有属性,则自动执行__unset()的方法 复制代码 代码如下: <?php     /**      * 针对类中的魔术方法 __isset() 和 __unset() 的例子      */ class Example {     public $public

  • php中is_null,empty,isset,unset 的区别详细介绍

    is_null, empty, isset, unset 我们先来看看这4个函数的描述. isset 判断变量是否已存在(配置)unset 把变量删除(释放)掉empty 判断变量是否为空is_null 判断变量是否为NULLok,已经开始搞人了.那么开始,这4个函数中除了unset,其他3个都是判断函数,unset首先出局,因为他不会搞错,其次是is_null,我们可以把它看成是!isset,是isset的一个逆操作,下面一张表可以很清楚的说明他们之间的关系: 复制代码 代码如下: 变量   

  • php unset全局变量运用问题的深入解析

    PHP unset全局变量在实际使用中只能销毁局部变量,并不能实现全局变量的目的.下面我们就来具体解决这一问题.希望对大家有所帮助. PHP中语言中有些函数对于初学者来说在字面上是很难理解的,需要在实际编码中总结经验,来掌握这些知识.我们今天要为大家介绍的是PHP unset全局变量在使用中遇到的一些问题. PHP 中有个释放变量的语句叫做unset(从PHP4开始unset已经不再是一个函数了,而是一个语句),前几天用unset的时候出了点问题,于是把 unset问题总结如下.如果您仔细阅读过

  • php中session_unset与session_destroy的区别分析

    session_unset() 释放当前在内存中已经创建的所有$_SESSION变量,但不删除session文件以及不释放对应的session id session_destroy() 删除当前用户对应的session文件以及释放session id,内存中的$_SESSION变量内容依然保留 因此,释放用户的session所有资源,需要顺序执行如下代码: 复制代码 代码如下: <?php $_SESSION['user'] = 'lowell'; session_unset(); sessio

  • PHP中使用unset销毁变量并内存释放问题

    复制代码 代码如下: for ( $i = 1; $i < 100; $i++ ) { $str = str_repeat('01234567', $i); $a = memory_get_usage(); unset($str); $b = memory_get_usage(); echo "\n ".$i.': '.($b - $a).' Bytes.'; } 从结果看出: 8 x 32 = 256 在256字节长的时候才真正有必要释放内存,有些人说,不如直接$str = n

  • 解析PHP中的unset究竟会不会释放内存

    首先让我们看一个例子 复制代码 代码如下: var_dump(memory_get_usage());    $a = "laruence";    var_dump(memory_get_usage());    unset($a);    var_dump(memory_get_usage()); 输出(在我的个人电脑上, 可能会因为系统,PHP版本,载入的扩展不同而不同):    int(90440)    int(90640)    int(90472 注意到 90472-90

  • 关于php unset对json_encode的影响详解

    前言 PHP 中有个释放变量的语句叫做unset(从PHP4开始unset已经不再是一个函数了,而是一个语句),本文主要给大家介绍了关于php unset对json_encode影响的相关内容,下面话不多说了,来一起看看详细的介绍吧 先运行一段php代码: $a = Array(0=>'hello world', 1=>'girl', 2=>'boy'); var_dump(json_encode($a)); unset($a[1]); var_dump(json_encode($a))

  • PHP中删除变量时unset()和null的区别分析

    第一种方法:$varname=null 第二种方法:unset($varname) 这两种方法都可以删除变量,但结果有些许的差别. 代码: 复制代码 代码如下: <?php $a = array( 'a' => 'a', 'b' => 'b' ); $b = array( 'a' => 'a', 'b' => 'b' ); $a['b'] = null; unset($b['b']); print('<pre>'); print_r($a); print('<

  • PHP unset函数原理及使用方法解析

    unset-释放给定的变量 说明 unset(mixed$var[,mixed$...] ) :void unset()销毁指定的变量. unset()在函数中的行为会依赖于想要销毁的变量的类型而有所不同. 如果在函数中unset()一个全局变量,则只是局部变量被销毁,而在调用环境中的变量将保持调用unset()之前一样的值. <?php function destroy_foo() { global $foo; unset($foo); } $foo = 'bar'; destroy_foo(

随机推荐