关于php unset函数的问题

发布于 2024-11-11 18:26:47 字数 223 浏览 4 评论 0原文

有疑问。

  1. 我对 unset如何取消设置所有变量 我应该使用 unset($var1,$var2,$var3,...) 或任何其他简单的方法?
  2. 在函数末尾取消设置变量是个好习惯吗?有什么区别!
  3. 取消设置变量是否会减少编程执行时间

谢谢

I am having questions about unset

  1. How to unset all variables.should i use unset($var1,$var2,$var3,...) or any other easy method exists ?
  2. unseting the variables at the end of functions is good practice?.any difference !
  3. unseting the variable is will reduce programming execution time or not ?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

孤者何惧 2024-11-18 18:26:47
  1. 您的意思是 unset($var1,$var2,$var3,...) 对您来说不够简单?

  2. 显式地这样做是没有意义的,因为局部变量总是会在函数作用域的末尾消失。这也适用于引用变量,只有函数本地的引用才会消失,但无论它们引用什么,如果在函数范围之外,仍然会存在。

  3. 不知道。

  1. You mean unset($var1,$var2,$var3,...) isn't easy enough for you?

  2. 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.

  3. No idea.

坠似风落 2024-11-18 18:26:47

如何取消设置所有变量。我应该使用 unset($var1,$var2,$var3,...) 还是存在任何其他简单的方法?

是的,这是取消设置多个变量的正常方法。您可以迭代范围内的变量并取消设置,但这太过分了。

在函数末尾取消设置变量是个好习惯吗?有什么区别!

虽然变量将在作用域(函数、类、脚本)结束时进行垃圾收集,但在单文件脚本(过程)中执行此操作可能很有用 - 特别是在其他脚本任意包含在作用域中的脚本中。

话虽如此,对于干净的组织来说,这是不必要的;然而,这也不一定是坏事。

取消设置变量是否会减少编程执行时间?

在大多数情况下,几乎没有差异;然而,正如我之前提到的,它不会造成伤害,并且有可能使范围内的内容和不包含的内容变得更加清晰。事实上,我通常在 for/foreach 之后立即执行此操作,因为 for/foreach 没有块作用域,因此这些块内定义的变量在循环后可用。

示例:

foreach ($doctors as $key => $val) {
    // do something
}
unset($key, $val);

顺便说一句,如果您想知道如何实际批量执行此操作(是的,这是可能的,但它并不漂亮):

<?php

$_SCRIPT_one   = 1;
$_SCRIPT_two   = 2;
$_SCRIPT_three = 3;

// list of all variables defined
$all   = array_keys(get_defined_vars());

// list only the local variables we are interested in
$local = array_filter($all, function($name) { return preg_match('/^_SCRIPT_/i', $name); });

// dump currently scoped local variables
var_dump($local);


// unset local variables
foreach ($local as $var) { unset($var); }


// list of all variables defined
$all   = array_keys(get_defined_vars());

// list only the local variables we are interested in
$local = array_filter($all, function($name) { return preg_match('/^_SCRIPT_/i', $name); });

// dump currently scoped local variables
var_dump($local);

How to unset all variables.should i use unset($var1,$var2,$var3,...) or any other easy method exists ?

Yes, this is the normal way to unset multiple variables. You could iterate the in-scope variables and unset, but that would be overkill.

unseting the variables at the end of functions is good practice?.any difference !

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.

unseting the variable is will reduce programming execution time or not ?

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:

foreach ($doctors as $key => $val) {
    // do something
}
unset($key, $val);

BTW, in case you want to know how to actually do this in bulk (yes, it is possible, but it isn't pretty):

<?php

$_SCRIPT_one   = 1;
$_SCRIPT_two   = 2;
$_SCRIPT_three = 3;

// list of all variables defined
$all   = array_keys(get_defined_vars());

// list only the local variables we are interested in
$local = array_filter($all, function($name) { return preg_match('/^_SCRIPT_/i', $name); });

// dump currently scoped local variables
var_dump($local);


// unset local variables
foreach ($local as $var) { unset($var); }


// list of all variables defined
$all   = array_keys(get_defined_vars());

// list only the local variables we are interested in
$local = array_filter($all, function($name) { return preg_match('/^_SCRIPT_/i', $name); });

// dump currently scoped local variables
var_dump($local);
感性 2024-11-18 18:26:47

首先,unset 是一种语言构造,而不是一个函数。

如何取消设置所有变量。我应该使用 unset($var1,$var2,$var3,...) 还是存在任何其他简单的方法?

您可以取消设置所有全局变量,但是,我不明白您为什么要这样做:

foreach (array_keys($GLOBALS) as $var) {
   if ($var != 'GLOBALS') unset($GLOBALS[$var]);
}

在函数末尾取消设置变量是个好习惯吗?有什么区别!

不,当变量超出范围时,它会自动取消设置。手动执行此操作没有意义。

取消设置变量是否会减少编程执行时间?

事实并非如此,但它可能会减少内存使用量。

First, unset is a language construct, not a function.

How to unset all variables.should i use unset($var1,$var2,$var3,...) or any other easy method exists ?

You can unset all global variables, however, I don't see why you'd do such a thing:

foreach (array_keys($GLOBALS) as $var) {
   if ($var != 'GLOBALS') unset($GLOBALS[$var]);
}

unseting the variables at the end of functions is good practice?.any difference !

No, the variable is automatically unset when it goes out of scope. There is no point doing it manually.

unseting the variable is will reduce programming execution time or not ?

Not really, it may reduce memory usage though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文