Questions about php unset function

[亡魂溺海] 提交于 2019-12-23 20:28:18

问题


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


回答1:


  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.




回答2:


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);



回答3:


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.



来源:https://stackoverflow.com/questions/6234732/questions-about-php-unset-function

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!