Servage Magazine

Information about YOUR hosting company – where we give you a clear picture of what we think and do!

Global and static PHP variables

Tuesday, June 24th, 2014 by Servage


Global variables

There are cases when you need a variable to have a global scope, because you want all your code to be able to access it. Also, some data may be large and complex, and you don’t want to keep passing it as arguments to functions.

To declare a variable as having global scope, use the keyword global. Let’s assume that you have a way of logging your users into your website and you want all your code to know whether it is interacting with a logged-in user or a guest. One way to do this is to create a global variable such as $is_logged_in:

global $is_logged_in;

Now your login function simply has to set that variable to “1” upon success of a login attempt, or “0” upon its failure. Because the scope of the variable is global, every line of code in your program can access it.

You should use global variables with caution, though. I recommend that you create them only when you absolutely cannot find another way of achieving the result you desire. In general, programs that are broken into small parts and segregated data are less buggy and easier to maintain. If you have a thousand-line program (and some day you will) in which you discover that a global variable has the wrong value at some point, how long will it take you to find the code that set it incorrectly?

Also, if you have too many global variables, you run the risk of using one of those names again locally, or at least thinking you have used it locally, when in fact it has already been declared as global. All kinds of strange bugs can arise from such situations.

Sometimes I adopt the convention of making all global variable names uppercase (just as it’s recommended that the names of constants should be uppercase) so that I can see at a glance the scope of a variable.

Static variables

I mentioned earlier that the value of the variable is wiped out when the function ends. If a function runs many times, it starts with a fresh copy of the variable each time and the previous setting has no effect.

Here’s an interesting case. What if you have a local variable inside a function that you don’t want any other parts of your code to have access to, but that you would also like to keep its value for the next time the function is called? Why? Perhaps because you want a counter to track how many times a function is called. The solution is to declare a static variable.

A function using a static variable

function test()
  static $count = 0;
  echo $count;

Here, the very first line of function test creates a static variable called $count and initializes it to a value of 0. The next line outputs the variable’s value; the final one increments it.

The next time the function is called, because $count has already been declared, the first line of the function is skipped. Then the previously incremented value of $count is displayed before the variable is again incremented.

If you plan to use static variables, you should note that you cannot assign the result of an expression in their definitions.

Superglobal variables

Starting with PHP 4.1.0, several predefined variables are available. These are known as superglobal variables, which means that they are provided by the PHP environment but are global within the program, accessible absolutely everywhere.

These superglobals contain lots of useful information about the currently running program and its environment.

Sources for further reading

Global and static PHP variables, 3.7 out of 5 based on 3 ratings
Categories: Tips & Tricks


You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

No comments yet (leave a comment)

You are welcome to initiate a conversation about this blog entry.

Leave a comment

You must be logged in to post a comment.