Solved

Unset all globals.

Posted on 2002-06-05
6
212 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

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…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

752 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