Results 1 to 2 of 2

Thread: $GLOBALS in a function

  1. #1
    Join Date
    Nov 2006
    Location
    Northeast USA
    Posts
    408
    Thanks
    8
    Thanked 30 Times in 28 Posts

    Default $GLOBALS in a function

    Hello,
    I am writing alot of functions for my website in php, and in almost every one, It requires a variable outside of the functions. So thats why the invented $GLOBALS. But, It gets to be a pain writing out $my_var = $GLOBALS['myvar']; for every stupid function, especially when there's over 20 that I need. And there is no way will I put all 20 variables in the calling perenthese.
    I tried a for-loop but every time I attempt, I always get: Phrase Error: da-da-da-da-da... Can someone please make a simple for loop that changes all globals into varables. Thanks in advance.
    ~FSD
    -Ben -- THE DYNAMIC DRIVERS
    My Links: My DD Profile||My Youtube Video Tutorials||DD Helping Coders||DD Coders In Training
    I told my client to press F5, the client pressed F, then 5, *facepalm*

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    foreach ($GLOBALS as $k=>$g) {
    $$k = $g;
    }

    to return them:
    foreach ($GLOBALS as $k=>$g) {
    $tempglobals[$k] = $$k;
    }
    $GLOBALS = $tempglobals;
    Last edited by djr33; 10-19-2007 at 10:38 PM.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •