取消设置变量与设置为“”
执行以下操作之一是否更好?如果不是,其中一个比另一个快吗?
unset($variable);
或做
$variable = '';
Is it better form to do one of the following? If not, is one of them faster than the other?
unset($variable);
or to do
$variable = '';
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
它们会做稍微不同的事情:
unset 将从符号表中删除变量,并将内容的引用计数减少 1。之后对变量的引用将触发通知(“未定义的变量”)。 (注意,对象可以通过实现 __unset() 来覆盖其属性上的默认未设置行为)。
设置为空字符串将使内容的引用计数减1,将内容设置为0长度的字符串,但符号仍保留在符号表中,并且您仍然可以引用该变量。 (注意,对象可以通过实现 __set() 来覆盖其属性上的默认赋值行为)。
在较旧的 php 中,当引用计数降至 0 时,将调用析构函数并立即释放内存。在较新的版本 (>= 5.3) 中,php 使用缓冲方案,可以更好地处理循环引用 (http://www.php.net/manual/en/features.gc.collecting-cycles.php),因此内存可能稍后被释放,但也可能不会完全被延迟......无论如何,这不会真正导致任何问题,并且新算法可以防止某些内存泄漏。
如果变量名称不会再次使用,则 unset 应该会快几个 cpu 周期(因为不需要创建新内容)。但是如果变量名被重新使用,php将不得不创建一个新的变量和符号表条目,所以它可能会更慢!在大多数情况下,差异可以忽略不计。
如果您想将该变量标记为无效以供以后检查,可以将其设置为 false 或 null。这比使用 isset() 进行测试要好,因为变量名称中的拼写错误将返回 false 而不会出现任何错误...您还可以将 false 和 null 值传递给另一个函数并保留哨兵值,这是不能用未设置的变量...
所以我会说:
或者
比检查哨兵值更好
they will do slightly different things:
unset will remove the variable from the symbol table and will decrement the reference count on the contents by 1. references to the variable after that will trigger a notice ("undefined variable"). (note, an object can override the default unset behavior on its properties by implementing __unset()).
setting to an empty string will decrement the reference count on the contents by 1, set the contents to a 0-length string, but the symbol will still remain in the symbol table, and you can still reference the variable. (note, an object can override the default assignment behavior on its properties by implementing __set()).
in older php's, when the ref count falls to 0, the destructor is called and the memory is freed immediately. in newer versions (>= 5.3), php uses a buffered scheme that has better handling for cyclical references (http://www.php.net/manual/en/features.gc.collecting-cycles.php), so the memory could possibly be freed later, tho it might not be delayed at all... in any case, that doesn't really cause any issues and the new algorithm prevents certain memory leaks.
if the variable name won't be used again, unset should be a few cpu cycles faster (since new contents don't need to be created). but if the variable name is re-used, php would have to create a new variable and symbol table entry, so it could be slower! the diff would be a negligible difference in most situations.
if you want to mark the variable as invalid for later checking, you could set it to false or null. that would be better than testing with isset() because a typo in the variable name would return false without any error... you can also pass false and null values to another function and retain the sentinel value, which can't be done with an unset var...
so i would say:
or
would be better for checking sentinel values than
从技术上讲,
$test = ''
将返回 true因为它仍然是“set”,所以它只是设置为空值。
然而它会返回 true,
因为它是一个空变量。这仅取决于您要检查的内容。一般来说,人们倾向于检查变量是否已设置,而不是检查它是否为空。
所以最好完全取消它。
更容易理解
另外,这比
第一个立即告诉您该变量不再设置 。后者只是告诉您它被设置为空白。当您要向变量添加内容并且不希望 PHP 出错时,通常会使用此方法。
Technically
$test = ''
will return true toBecause it is still 'set', it is just set to en empty value.
It will however return true to
as it is an empty variable. It just depends on what you are checking for. Generally people tend to check if a variable isset, rather than if it is empty though.
So it is better to just unset it completely.
Also, this is easier to understand
than this
the first immediately tells you that the variable is NO LONGER SET. Where as the latter simply tells you it is set to a blank space. This is commonly used when you are going to add stuff to a variable and don't want PHP erroring on you.
您正在做不同的事情,
unset
的目的是在创建它的上下文中销毁指定的变量,您的第二个示例只是将变量设置为空字符串。取消设置变量不会强制立即释放内存,如果您担心性能,将变量设置为 NULL 可能是一个更好的选择,但实际上,差异并不明显......
中讨论
文档
:You are doing different things, the purpose of
unset
is to destroys the specified variable in the context of where you make it, your second example simply sets the variable to an empty string.Unsetting a variable doesn't force immediate memory freeing, if you are concerned about performance, setting the variable to
NULL
may be a better option, but really, the difference will be not noticeable...Discussed in the
docs
:我认为最相关的区别是,取消设置变量表明该变量不会被后续代码使用(如果您尝试使用它,它也会通过报告 E_NOTICE 来“强制”这一点,正如 jspcal 所说,这是因为它不在符号中表不再)。
因此,如果空字符串是您对变量所做的任何操作的合法(或哨兵)值,请继续将其设置为“”。否则,如果该变量不再有用,则取消设置它可以使代码意图更清晰。
I think the most relevant difference is that unsetting a variable communicates that the variable will not be used by subsequent code (it also "enforces" this by reporting an E_NOTICE if you try to use it, as jspcal said that's because it's not in the symbol table anymore).
Therefore, if the empty string is a legal (or sentinel) value for whatever you are doing with your variable, go ahead and set it to ''. Otherwise, if the variable is no longer useful, unsetting it makes for clearer code intent.
它们具有完全不同的含义。前者使变量不存在。后者只是将其值设置为空字符串。可以这么说,哪一个“更好”并不重要,因为它们用于完全不同的事情。
你是想清理内存还是什么?如果是这样,就不要这样做; PHP 为您管理内存,因此您可以将其闲置,它会自动清理。
如果您不想清理内存,那么您需要弄清楚为什么要取消设置变量或将其设置为空,然后选择合适的变量。一个很好的健全性检查:假设有人在你的 unset/empty 之后插入了以下代码行:
然后,稍后:
如果你将变量设置为空,第一个将运行
do_something()
细绳。如果您取消设置该变量,第二个将运行do_something_else()
。如果您的脚本运行正常,您希望运行以下哪一个?They have totally different meanings. The former makes a variable non-existant. The latter just sets its value to the empty string. It doesn't matter which one is "better" so to speak, because they are for totally different things.
Are you trying to clean up memory or something? If so, don't; PHP manages memory for you, so you can leave it laying around and it'll get cleaned up automatically.
If you're not trying to clean up memory, then you need to figure out why you want to unset a variable or set it to empty, and choose the appropriate one. One good sanity check for this: let's say someone inserted the following line of code somewhere after your unset/empty:
And then, later:
The first will run
do_something()
if you set the variable to the empty string. The second will rundo_something_else()
if you unset the variable. Which of these do you expect to run if your script is behaving properly?这里还有一个需要考虑的“问题”,即参考文献。
如果你有:
那么做你的两种选择中的任何一种都是完全不同的。
将 $variable 和 $a 设置为空字符串,其中 as
删除 $a 和 $variable 之间的引用链接,同时从符号表中删除 $variable。这确实是取消 $a 和 $variable 的链接而不将 $variable 设置为引用其他内容的唯一方法。请注意,例如 $variable = null;不会做的。
There is one other 'gotcha' to consider here, the reference.
if you had:
then to do either of your two alternatives is quite different.
sets both $variable and $a to the empty string, where as
removes the reference link between $a and $variable while removing $variable from the symbol table. This is indeed the only way to unlink $a and $variable without setting $variable to reference something else. Note, e.g., $variable = null; won't do it.