开发者

Questions about php unset function

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 ti开发者_StackOverflowme or not ?

Thanks


  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.


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜