Solved

Unset all globals.

Posted on 2002-06-05
6
194 Views
Last Modified: 2006-11-17
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
Comment
Question by:jhess
  • 3
  • 3
6 Comments
 

Author Comment

by:jhess
Comment Utility
Here is a link to the similar question - http://www.experts-exchange.com/questions/Q_20146662.html
0
 
LVL 5

Expert Comment

by:Hamlet081299
Comment Utility
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
 
LVL 5

Accepted Solution

by:
Hamlet081299 earned 50 total points
Comment Utility
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

by:jhess
Comment Utility
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
 

Author Comment

by:jhess
Comment Utility
See my last comment.
0
 
LVL 5

Expert Comment

by:Hamlet081299
Comment Utility
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

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now