memory - Unset all variables in PHP script -
trying unset automatically variables in script.
have tried way:
echo '<br /> variables in script before unset(): <br />'; print_r(array_keys(get_defined_vars())); echo '<br /><br />'; var_dump(get_defined_vars()); // creates string of comma-separated variables(*) unset. $all_vars = implode(', $', array_keys(get_defined_vars())); echo '<br /><br />'; echo '<br />list variables in script: <br />'; echo $all_vars; unset($all_vars); echo '<br /><br />'; echo '<br />variables in script after unset(): <br />'; print_r(array_keys(get_defined_vars())); echo '<br />'; var_dump(get_defined_vars());
why not work?
is there improve way this?
thanks helping!
(*) it's seems not create variables, string looks variables...
here ya go ->
$vars = array_keys(get_defined_vars()); ($i = 0; $i < sizeof($vars); $i++) { unset($$vars[$i]); } unset($vars,$i);
and clarify, implode returns "a string representation of array elements in same order". http://php.net/manual/en/function.implode.php
unset requires actual variable parameter, not string representation. similiar get_defined_vars() returns (not actual variable reference). code goes through array of strings, , returns each reference using $ in front end - unset can use.
php memory memory-management memory-leaks unset
No comments:
Post a Comment