Unset all variables in PHP script

foreach (array_keys($GLOBALS) as $k) unset($$k); unset($k);


Here ya go ->

$vars = array_keys(get_defined_vars());
for ($i = 0; $i < sizeOf($vars); $i++) {
    unset($$vars[$i]);
}
unset($vars,$i);

And to clarify, implode returns "a string representation of all the array elements in the same order". http://php.net/manual/en/function.implode.php

Unset requires the actual variable as a parameter, not just a string representation. Which is similiar to what get_defined_vars() returns (not the actual variable reference). So the code goes through the array of strings, and returns each as a reference using the extra $ in front - which unset can use.


don't know about you guys, but $$vars doesn't work for me.

that's how I did it.

$vars = array_keys(get_defined_vars());
foreach($vars as $var) {
    unset(${"$var"});
}