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