Global variables in PHP · 17 October 2007, 17:35

There are two main ways to refer to a global variable from within a function. The first is by declaring the variable global in the function scope before accessing it:

$var = "This variable contains some interesting content";
function accessGlobal() {
  global $var;
  // Do something with $var
}

The second is to refer to the $GLOBALS superglobal:

$var = "This variable contains some interesting content";
function accessGlobal() {
  // Do something with $GLOBALS['var']
}

To skip to the point – neither is a good idea, but latter is the better practise of the two.

Know your variable usage

I will always suggest using the following construct to declare and use global variables:

$GLOBALS['var'] = "This variable contains some interesting content";
function accessGlobal() {
  // Do something with $GLOBALS['var']
}

I call this scope-aware programming. It may seem like it’s just more writing than the other variants, but flexibility is lacking if globals are not expressly declared as global. We live in a time of plug-ins… we’re rarely going to be responsible for a stand-alone PHP application.

A plug-in shouldn’t have to rely on lying in any particular scope to function. And it’s quite easy to see why, too. Let’s assume you’re coding for any of the more popular content management systems. Chances are that your file will be included during a loop over the content… which sounds harmless at first, and, indeed, is, but this loop is – in a large majority of cases – found in a function.

With this in mind, our previous examples look a bit different than they did before.

global $var => $var

function iterateOverContent() {
  $var = "This variable contains some interesting content";
  function accessGlobal() {
    global $var;
    // Do something with $var
  }
}

accessGlobal() breaks here, because it is trying to read a global variable that doesn’t exist. $var would have to be declared global in the ‘global’ scope of the included script, so that it ‘drops’ out of the function iterateOverContent() and becomes global, thus letting other functions refer to it.

$GLOBALS[‘var’] => $var

function iterateOverContent() {
  $var = "This variable contains some interesting content";
  function accessGlobal() {
    // Do something with $GLOBALS['var']
  }
}

accessGlobal() breaks for the same reasons – $var isn’t global, so it obviously isn’t to be found in the $GLOBALS array, either. “So, wait,” you ask, “How is this better than using global $var if it breaks anyway?”

It shows marginally more foresight. Whereas in example one, you’d be forced to go through your entire document to replace $var with $GLOBALS[‘var’] while making sure that you do not accidentally falsely overwrite a variable by the same name in some function’s scope, using $GLOBALS[‘var’] to access the global variable lets you focus on just replacing its declaration.

That’s a far less error prone conversion.

Of course, if you have this much insight, you might aswell take the last, tiny step, and code abiding to the best concept:

$GLOBALS[‘var’] => $GLOBALS[‘var’]

function iterateOverContent() {
  $GLOBALS['var'] = "This variable contains some interesting content";
  function accessGlobal() {
    // Do something with $GLOBALS['var']
  }
}

This will always work, and saves you the headache of trying to keep track of either differing variable names, or which global variable you’ve ‘imported’ into the function and which you haven’t, to boot.

Another added benefit is one for the enclosing program (the one including your code): the $GLOBALS[] array can be easily preserved across the inclusion, meaning that you can’t wreck its code with bad naming conventions:

function iterateOverContent() {
  $preserve = array_copy($GLOBALS);
  include('your_plug_in.php');
  $GLOBALS = $preserve;
}

That doesn’t break your script, but ensures nothing else can break, either. (Note – of course you can preserve several global variables that are scattered about, too. There are methods in PHP that let you access the variables of a scope and put them back again – but that’s far removed from ‘clean’.)

Incidentally, the PHP website recommends $GLOBALS since PHP4.

— Neike Taika-Tessaro

---

Comment

Textile Help
Categories: development , knowledge , php
Related:

Beating a dead horse: Jurassic Park raptors · 27 December 2008, 03:23

mIRC Script: Opping yourself in all channels in a single go · 14 December 2008, 21:48

mIRC Script: Flexible moderation · 14 December 2008, 21:27

mIRC cut script · 8 December 2008, 09:22

opacity for fades · 17 October 2007, 16:52