Solved

Unset all globals.

Posted on 2002-06-05
6
203 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
ID: 7057587
Here is a link to the similar question - http://www.experts-exchange.com/questions/Q_20146662.html
0
 
LVL 5

Expert Comment

by:Hamlet081299
ID: 7058383
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
ID: 7058388
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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

Author Comment

by:jhess
ID: 7059555
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
ID: 7059558
See my last comment.
0
 
LVL 5

Expert Comment

by:Hamlet081299
ID: 7060483
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

786 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