Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 246
  • Last Modified:

Unset all globals.

I want to simply delete all global in the global scope.

There was another question just like this posted earlier, but the answer is incorrect. The proposed solution only deletes all entries of the $GLOBALS array in a function but the global vars themselves are not affected.


A correct solution would allow this statement:
print_r(get_defined_vars());

To yield: "array - ()"

Any ideas?
0
jhess
Asked:
jhess
  • 3
  • 3
1 Solution
 
jhessAuthor Commented:
Here is a link to the similar question - http://www.experts-exchange.com/questions/Q_20146662.html
0
 
Hamlet081299Commented:
I tried the code as at the similar question and it seems to me like it does work.  

Can you give an example that demostrates where it doesn't?
0
 
Hamlet081299Commented:
Okay ... try the code as per the example, but add unset($key) at then end...

foreach (array_keys($GLOBALS) as $key) {
    unset($$key);
}
unset($key);
print_r(get_defined_vars());
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
jhessAuthor Commented:
Wow. My version wasn't useing the array_keys() function i was just saying

foreach($GLOBALS as $key => $value) unset($key);

And it would just hang forever.... It seemed to always hang if i accessed $GLOBALS from the global scape.


Oh man now i see my mistake. I was unsetting $key and not was key was "pointing" to. And that probably explains why it looped forever too...

Thanks guys -
Jon Hess
0
 
jhessAuthor Commented:
See my last comment.
0
 
Hamlet081299Commented:
I think the reason it looped forever is because each time through the loop you are setting two variables $key and $value, and as a result it never got to the end of the $GLOBALS array.

I'm not sure why the problem doesn't happen with just one var ($key).  In fact in one version I added a check for this, but it turned out to be unnecessary (at least in my version of PHP)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now