Fork me on GitHub

article

I guess I learned it wrong

April 21, 2005 | PHP Scripting

Globals.

When I first started learning PHP it was well after the push to ship PHP with register_globals off by default, so I never learned to use them. However, I also thought the $GLOBALS variable was off limits as well.

I was wrong.

It seems that this wonderful little variable can do more to help with variable scope than I ever thought possible.

Consider this code.

$var = "global";
function edit($n)
    {
        $GLOBALS['var'] = $n." edit";
    }
edit($var);
echo($var);

This code returns ‘global edit’ when run. The $GLOBALS array is only made up of variables defined and set by the script – it doesn’t contain super globals like POST and GET vars and thus are safe from code contamination that might occur from someone trying to change variable content in your scripts.

Wow. I’ve got a whole new world set out in front of me now that I’ve learned references and that $GLOBALS are available.

Maybe I should be a little embarrassed that it took me this long to discover these two basic, basic gems of PHP but I’m not. For being self taught I’m not doing too bad.

Now, off to learn OOP.

7 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  • You’re blogging about stuff that I don’t thoroughly grasp in php. Having said that, in this example, could edit() be writted as

    function edit($n) {
     global $var;
     $var .= $n;
     }
    

    I guess what I’m getting at is that in your example, does $var retain its original value of "global" where as in mine $var’s value is altered?

    John Pennypacker, April 22, 2005 5:49 am | permalink

  • heh, I goofed that up.

    I should have written

    function edit($n) {
    global $var;
    $var .= " edit";
    }
    

    I guess what I’m asking is that is $var a separate entity from $GLOBALS[‘var’]?

    John Pennypacker, April 22, 2005 5:52 am | permalink

  • No, they are one and the same, kinda like the reference article before this one.

    When you set $var, $GLOBAL[‘var’] is then created. So, I guess you could say that $GLOBAL[‘var’] is a reference to $var.

    Shawn Parker, April 22, 2005 6:05 am | permalink

  • Also, if you change $var, $GLOBAL[‘var’] changes and vice versa.

    Shawn Parker, April 22, 2005 6:07 am | permalink

  • gotcha.

    Is there any significant advantage to stating $GLOBAL[‘var’] vs. global $var?

    John Pennypacker, April 22, 2005 6:41 am | permalink

  • Not that I can see as of yet, other than $GLOBAL gets set automatically when you define a var outside a function.

    global $var probably is the most useful when defining a new var inside a function and wanting it to be available outside the function.

    I do that often when a function has to return a bunch of different info, I’ll set the new vars as global $var.

    Shawn, April 22, 2005 7:17 am | permalink

  • The really scary thing about this is that it is a basic concept. So are references. Its a fucking wonder I’ve gotten this far having missed basic PHP concepts like this.

    Shawn, April 22, 2005 2:36 pm | permalink

Comments are closed