Questions about php undo function

I have questions about unset

  • How to disable all variables. Should I use unset($var1,$var2,$var3,...)or is there any other simple method that exists?
  • Is opacity of variables at the end of functions a good practice?
  • Invalid variable - is it reduceprogramming execution timeor not?

thank

+3
source share
3 answers
  • You want to say that unset($var1,$var2,$var3,...)it’s not easy for you?

  • It makes no sense to do this explicitly, since local variables will always disappear at the end of the function area. This also applies to reference variables, only links local to the function will disappear, but all that they reference, if outside the scope of the function, will still be.

  • I do not know.

+3
source

. unset ($ var1, $var2, $var3,...) ?

, . , .

- ?. !

, (, , script), script () - , .

, ; , .

- ?

; , , , / . , for/foreach, /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);
+1

-, unset - , .

. unset ($ var1, $var2, $var3,...) ?

, , ​​:

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

- ?. !

No, a variable is automatically disabled when it goes out of scope. It makes no sense to do it manually.

An invalid variable is a reduction in program execution time or not?

In fact, this can reduce memory usage.

0
source

All Articles