Jump to content

PHP unsetting all variables


tibberous

Recommended Posts

$keys = array();

foreach($GLOBALS as $k => $v){
	$keys[] = $k;
}

for($t=1;$keys[$t];$t++){
	unset($$keys[$t]);
}
unset($k); unset($v); unset($t);

 

Yup - un-fucking-set.

 

Hopefully Google spiders this and makes it the top result, because I spent forever looking and couldn't find this - had to figure it our myself =/

 

And in case your wonder why you'd ever need this - I'm using it for a template system, to wipe out all of the variables defined before the templates index file gets included.

Link to comment
https://forums.phpfreaks.com/topic/223400-php-unsetting-all-variables/
Share on other sites

I can write that code in two lines ...

 

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

 

of course, you realize that $GLOBALS is now gone, and you can't really get it back. And so is $_GET, $_POST, $_SERVER, etc., etc., ... Hell, I'm not really sure I'm still here!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.