关于php unset函数的问题
有疑问。
- 我对
unset
如何取消设置所有变量 我应该使用unset($var1,$var2,$var3,...)
或任何其他简单的方法? - 在函数末尾取消设置变量是个好习惯吗?有什么区别!
- 取消设置变量是否会
减少
编程执行时间
?
谢谢
I am having questions about unset
- How to unset all variables.should i use
unset($var1,$var2,$var3,...)
or any other easy method exists ? - unseting the variables at the end of functions is good practice?.any difference !
- unseting the variable is will
reduce
programmingexecution time
or not ?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的意思是
unset($var1,$var2,$var3,...)
对您来说不够简单?显式地这样做是没有意义的,因为局部变量总是会在函数作用域的末尾消失。这也适用于引用变量,只有函数本地的引用才会消失,但无论它们引用什么,如果在函数范围之外,仍然会存在。
不知道。
You mean
unset($var1,$var2,$var3,...)
isn't easy enough for you?There's no point to doing so explicitly since local variables will always disappear at the end of a function's scope. This applies to reference variables too, only the references local to the function will disappear, but whatever they reference, if outside the function scope, will still be there.
No idea.
是的,这是取消设置多个变量的正常方法。您可以迭代范围内的变量并取消设置,但这太过分了。
虽然变量将在作用域(函数、类、脚本)结束时进行垃圾收集,但在单文件脚本(过程)中执行此操作可能很有用 - 特别是在其他脚本任意包含在作用域中的脚本中。
话虽如此,对于干净的组织来说,这是不必要的;然而,这也不一定是坏事。
在大多数情况下,几乎没有差异;然而,正如我之前提到的,它不会造成伤害,并且有可能使范围内的内容和不包含的内容变得更加清晰。事实上,我通常在 for/foreach 之后立即执行此操作,因为 for/foreach 没有块作用域,因此这些块内定义的变量在循环后可用。
示例:
顺便说一句,如果您想知道如何实际批量执行此操作(是的,这是可能的,但它并不漂亮):
Yes, this is the normal way to unset multiple variables. You could iterate the in-scope variables and unset, but that would be overkill.
While variables will be garbage collected at the end of the scope (function, class, script), it may be useful to do this in a single-file script (procedural) -- especially in scripts where other scripts are included into scope arbitrarily.
That being said, with clean organization, this is unnecessary; however, not necessarily a bad thing either.
In most cases, there will be little to no difference; however, as I mentioned earlier, it can't hurt and could potentially stand to bring some clarity around what is/isn't in scope. In fact, I commonly do this right after a for/foreach since for/foreach doesn't have a block scope, so the variables defined inside those blocks are available after the loop.
Example:
BTW, in case you want to know how to actually do this in bulk (yes, it is possible, but it isn't pretty):
首先,
unset
是一种语言构造,而不是一个函数。您可以取消设置所有全局变量,但是,我不明白您为什么要这样做:
不,当变量超出范围时,它会自动取消设置。手动执行此操作没有意义。
事实并非如此,但它可能会减少内存使用量。
First,
unset
is a language construct, not a function.You can unset all global variables, however, I don't see why you'd do such a thing:
No, the variable is automatically unset when it goes out of scope. There is no point doing it manually.
Not really, it may reduce memory usage though.