16

Trying to unset automatically all variables in script.

Have tried this 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(*) for 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 does it not work?

Is there a better way to do this?

Thanks for helping!

(*) It's seems somewhat that it does not really create the variables, but a string that looks like variables...

4
  • 3
    Why do you need to do it at all?
    – user1864610
    Commented Oct 8, 2014 at 1:45
  • 1
    i think you've misunderstood the use of implode function.. Commented Oct 8, 2014 at 1:57
  • There isn't much reason to unset all variables. Memory management in php is like C or C++ where allocated memory needs to be freed. PHP will free all used memory automatically after the script is executed. Commented Oct 8, 2014 at 2:05
  • 1
    Just for anyone's knowledge: the reason I'm doing it is because our system is coded fairly poorly and we must include() file after file for a "report mailing" routine where we pretend we've run a report by giving them the $_POST they expect. Since it's an include, variables are stuffed into the global scope and we should clean them up before loading another (unrelated) report. I'm interested in others' use cases for this code. Commented Jan 22, 2015 at 18:15

4 Answers 4

18

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.

6
  • This doesn't work for me. I don't think $vars gets assigned by reference. Aren't you just unsetting the copy in the vars variable while leaving the original untouched? Commented Oct 8, 2014 at 2:09
  • There ya go - it's corrected. Sorry, I thought I had it working (something was working - just not sure what) :)
    – airtech
    Commented Oct 8, 2014 at 2:17
  • Ash501, just an FYI - that code snippet does not work.
    – airtech
    Commented Oct 8, 2014 at 2:19
  • @airtech - Yes you're right, yours works now -Thanks! was trying to avoid the loop. Left me wondering why mine does not work...?
    – Ash501
    Commented Oct 8, 2014 at 2:22
  • I updated your code to skip the $i variable, as it was getting included in the unset and thereby looping forever! :} Commented Jan 22, 2015 at 18:50
7

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"});
}
3

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

1
  • 2
    Just a note: This must be run in the global scope - using this in function will throw errors! Better to use unset($GLOBALS[$k]) instead of unset($k).
    – user5147563
    Commented Sep 14, 2017 at 19:05
-1

I couldn't run @airtech's answer on my PHP 7.0 instance. It throws an array to string conversion error.

This updated/simplified version of his solution works for me (I also added curly braces "{ }" for more clarity):

foreach(array_keys(get_defined_vars()) as $strVarName){
  unset(${$strVarName});
}

To answer the original question, as others stated, implode would just create a string of what looks like variables but wouldn't be interpreted as such by PHP.

The code solving the issue makes use of the "variable variables" concept in PHP. More on this concept here: https://www.php.net/manual/en/language.variables.variable.php

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.