Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Unset all globals.

Posted on 2002-06-05
6
Medium Priority
?
230 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 200 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

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.

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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 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 …
Suggested Courses

885 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