unset() - php 变量处理函数
unset()
(PHP 4, PHP 5, PHP 7)
释放给定的变量
说明
unset(mixed $var[,mixed$...
]): voidunset()销毁指定的变量。
unset()在函数中的行为会依赖于想要销毁的变量的类型而有所不同。
如果在函数中unset()一个全局变量,则只是局部变量被销毁,而在调用环境中的变量将保持调用unset()之前一样的值。
以上例程会输出:
bar
如果您想在函数中unset()一个全局变量,可使用$GLOBALS数组来实现:
如果在函数中unset()一个通过引用传递的变量,则只是局部变量被销毁,而在调用环境中的变量将保持调用unset()之前一样的值。
以上例程会输出:
something something
如果在函数中unset()一个静态变量,那么在函数内部此静态变量将被销毁。但是,当再次调用此函数时,此静态变量将被复原为上次被销毁之前的值。
以上例程会输出:
Before unset: 1, after unset: 23 Before unset: 2, after unset: 23 Before unset: 3, after unset: 23
参数
$var要销毁的变量。
...其他变量……
返回值
没有返回值。
范例
Example #1unset()示例
使用(unset)类型强制转换
(unset)类型强制转换常常和函数unset()引起困惑。为了完整性,(unset)是作为一个NULL类型的强制转换。它不会改变变量的类型。
以上例程会输出:
NULL string(6) "Felipe"
注释
Note:因为是一个语言构造器而不是一个函数,不能被可变函数调用。Note:
It is possible to unset even object properties visible in current context.Note:
在 PHP 5 之前无法在对象里销毁$this。Note:
在unset()一个无法访问的对象属性时,如果定义了__unset()则对调用这个重载方法。
参见
isset()
检测变量是否已设置并且非 NULLempty()
检查一个变量是否为空- __unset()
array_splice()
去掉数组中的某一部分并用其它值取代
This is probably trivial but there is no error for unsetting a non-existing variable.
You don't need to check that a variable is set before you unset it. is harmless. is pointless complication. This doesn't apply to properties of objects that have __isset() methods that visibly change object state or __unset() methods that don't properly check their arguments or have extra side effects. The latter case means that __unset shouldn't do more than what it says on the tin, and also has the responsibility for checking (possibly using __isset()) that what it's being asked to do makes sense. The former case is just plain bad design.
Since unset() is a language construct, it cannot be passed anything other than a variable. It's sole purpose is to "unset" this variable, ie. to remove it from the current scope and destroy it's associated data. This is true especially for reference variables, where not the actual value is destroyed but the reference to that value. This is why you can't wrap 'unset()' in a user defined function: You would either unset a copy of the data if the parameter is passed by value, or you would just unset the reference variable within the functions scope if the parameter is passed by reference. There is no workaround for that, as you cannot pass 'scope' to a function in PHP. Such a function can only work for variables that exist in a common or global scope (compare 'unset($_GLOBALS[variable])'). I don't know how PHP handles garbage collection internally, but I guess this behavior can result in a huge memory leak: if a value variable goes out of scope with a second variable still holding a reference to the in-memory value, then unsetting that reference would still hold the value in memory but potentially unset the last reference to that in-memory data, hence: occupied memory that is rendered useless as you cannot reference it anymore.
A sample how to unset array elements from an array result coming from a mysql request. In this sample it is checking if a file exists and removes the row from the array if it not exists. variables: mysql table = documents, array = $documents array key (index) = $key array row (record sort of speak) = $row explanation: 1. it gets the array from the table (mysql) 2. foreach goes through the array $documents 3. unset if record does not exist 4. the array_values($documents) reindexes the $documents array, for otherwise you might end up in trouble when your process will start expecting an array starting with key ($key) 0 (zero).
Here is another way to make 'unset' work with session variables from within a function : May it work with others than me... F.
if you try to unset an object, please be careful about references. Objects will only free their resources and trigger their __destruct method when *all* references are unsetted. Even when they are *in* the object... sigh!
In addition to what timo dot hummel at 4fb dot de said; >For the curious: unset also frees memory of the variable used. > >It might be possible that the in-memory size of the PHP Interpreter isn't reduced, but your scripts won't touch the memory_limit boundary. Memory is reused if you declare new variables. It might be worth adding that functions apparently don't free up memory on exit the same way unset does.. Maybe this is common knowledge, but although functions destroys variables on exit, it (apparently) doesn't help the memory. So if you use huge variables inside functions, be sure to unset them if you can before returning from the function. In my case, if I did not unset before return, then the script would use 20 MB more of memory than if I did unset. This was tested with php 5.0.4 on apache 2 on windows xp, with no memory limit. Before I did the test, I was under the impression that when you exit from functions, the memory used inside it would be cleared and reused. Maybe this should be made clear in the manual, for either unset() or in the chapter for functions.
Despite much searching, I have not yet found an explanation as to how one can manually free resources from variables, not so much objects, in PHP. I have also seen many comments regarding the merits and demerits of unset() versus setting a variable to null. Thus, here are the results of some benchmarks performed comparing unset() of numerous variables to setting them to null (with regards to memory usage and processing time): 10 variables: Unset: Memory Usage: 296 Time Elapsed: 1.0013580322266E-5 Null set: Memory Usage: 1736 Time Elapsed: 5.9604644775391E-6 50 variables: Unset: Memory Usage: 296 Time Elapsed: 3.6001205444336E-5 Null set: Memory Usage: 8328 Time Elapsed: 3.2901763916016E-5 100 variables: Unset: Memory Usage: 296 Time Elapsed: 5.6982040405273E-5 Null set: Memory Usage: 15928 Time Elapsed: 5.8174133300781E-5 1000 variables: Unset: Memory Usage: 296 Time Elapsed: 0.00041294097900391 Null set: Memory Usage: 168096 Time Elapsed: 0.00067591667175293 10000 variables: Unset: Memory Usage: 296 Time Elapsed: 0.0042569637298584 Null set: Memory Usage: 1650848 Time Elapsed: 0.0076930522918701 100000 variables: Unset: Memory Usage: 296 Time Elapsed: 0.042603969573975 Null set: Memory Usage: 16249080 Time Elapsed: 0.087724924087524 300000 variables: Unset: Memory Usage: 296 Time Elapsed: 0.13177299499512 Null set: Memory Usage: 49796320 Time Elapsed: 0.28617882728577 Perhaps my test code for the null set was flawed, but despite that possibility it is simple to see that unset() has minimal processing time impact, and no apparent memory usage impact (unless the values returned by memory_get_usage() are flawed). If you truly care about the ~4 microseconds saved over RESULT: "Notice: Undefined index: Bar" On PHP5 no error is raised, which seems to me like the correct behaviour. Note that using unset($foo['Bar']) in the above example does not generate a warning in either version. (Tested on 4.4.9 and 5.2.4)
Just to confirm, USING UNSET CAN DESTROY AN ENTIRE ARRAY. I couldn't find reference to this anywhere so I decided to write this. The difference between using unset and using $myarray=array(); to unset is that obviously the array will just be overwritten and will still exist. Output with unset is:
dh at argosign dot de - it is possible to unset globals from within functions thanks to the $GLOBALS array:
鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com
图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!