Global variables in PHP

We've all heard arguments both for and against global variables, especially in PHP. I'd like to start out by saying I'm not going to take a stand either for or against these variables (I'm not discriminatory to either the locals or the globals), just provide a guide as to their use, if you choose to implement them.

Global variables in PHP are those variables that can be accessed from any area of your code. In the main scope, and more importantly, from within functions. This is interesting to note (and when you stop to think of it, makes sense) as opposed to a client-side language such as javascript in which global variables are automatically accessible to functions. Being executed server-side, variable scope is vastly more important. Therefore, functions DO NOT automatically have access to global variables.

Declaring global variables

So, how do you create a global variable? Quite simple, actually. Just as you would any other variable, for example:

$newVariable = TRUE;

I have also found it helpful (on the suggestion of a friend) to use a common naming scheme for my global variables, so you (and everyone else that may modify your code) know you intend to use that variable in a global manner. The convention I use is as follows, with the "g_" notation preceding the variable name:

$g_newVariable = TRUE;

Accessing global variables in the local scope

Setting a global variable is the easy part. Using the global variable in a function can be a bit tricky if you're not ready for it. There are two primary methods used to access a global variable, as well as a third you can used if you're REALLY concerned about performance. So, here they are (assuming we've already assigned "g_newVariable":

function accessMethod01() {
  global $g_newVariable;
  $g_newVariable = FALSE;

function accessMethod02() {
  $GLOBALS['g_newVariable'] = FALSE;

function accessMethod03() {
  $tempVariable =& $GLOBALS['g_newVariable'];
  $tempVariable = FALSE;

In accessMethod01(), stating the variable is global will cause the function to look in the global scope for the definition. Any access of the variable afterward will be correctly handled.

In accessMethod02(), the global variable is accessed using the $GLOBALS associative array. $GLOBALS is a built in php superglobal that keeps track of all global variables by their key. This would be the easiest method if you only need to access a global array once during the lifetime if your function.

In accessMethod03(), the global variable is referenced by the local variable. This prevents multiple accesses to the $GLOBALS array and may give the performance boost I mentioned earlier.

Alternatives to global variables

There are a few alternatives to global variables, most of which make use of functions to interact with your global variables (think getters and setters in Objective C, which we'll get to later on down the road). However, for simple use, you're pretty much covered here. One site in particular I found useful that laid out a lot of ways to use global variables was the following on Bao's Blog.

I'd love to hear about how you're working with global variables in PHP (or other languages)! If I've misrepresented any information here (or you have something to add), please let me know.

Share Comment on Twitter