I'm using the following script in PHP 5.5.9 to unset all variables, which works great
$vars = array_keys(get_defined_vars());
for ($i = 0; $i < sizeOf($vars); $i++) {
unset($$vars[$i]); //this is line 72
}
unset($vars,$i);
However, in PHP 7, they give the following messages:
PHP Notice: Array to string conversion in /root/script.php on line 72
PHP Notice: Undefined variable: Array in /root/script.php on line 72
My question is how to make the script work in PHP 7?
Thanks for any suggestion!
You can avoid those errors by using foreach
instead of for
.
$vars = array_keys(get_defined_vars());
foreach ($vars as $var) {
unset($$var);
}
unset($vars, $var);
The order of evaluation of $$vars[$i]
is different in PHP 7. It's now strictly left to right.
Previously it would have first evaluated $vars[$i]
and then formed a new variable from the result of that with $
.
Now it first evaluates $$vars
and then tries to find [$i]
in the result of that.