dotkpay Posted August 29, 2011 Share Posted August 29, 2011 Hello, Am writing a script with lots of variables and its called by require() into the main file. But I probably have an interference in variable values because I get incorrect results. I have to unset all variables first before I enter the new script but there are lots of them. Is there a way I can unset all variables with a simple line of code or function? Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/245967-unset-all-variables/ Share on other sites More sharing options...
voip03 Posted August 29, 2011 Share Posted August 29, 2011 This is not a good programming practice, but if you want the job to be done... <?php $variables = array_keys(get_defined_vars()); foreach($variables as $variable) { unset(${"$variable"}); // You can reset them to empty string - ${"$variable"} = ""; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/245967-unset-all-variables/#findComment-1263255 Share on other sites More sharing options...
voip03 Posted August 29, 2011 Share Posted August 29, 2011 use unset http://php.net/manual/en/function.unset.php unset($variable1, $variable2, $variable3); OR try this one foreach (array_keys($GLOBALS) as $variables) unset($$variables);unset($variables); Quote Link to comment https://forums.phpfreaks.com/topic/245967-unset-all-variables/#findComment-1263262 Share on other sites More sharing options...
cunoodle2 Posted August 29, 2011 Share Posted August 29, 2011 Can you create a new file called "unset_variables.php" In there unset each variable you think you are ever using. Then (now and in the future) if you ever want to unset everything you can just do.. require_once unset_variables.php That work? Quote Link to comment https://forums.phpfreaks.com/topic/245967-unset-all-variables/#findComment-1263271 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.