PHP Super Globals

In JavaScript the scope of a variable is only limited in one direction. A variable defined globally is available from anywhere while a variable defined inside a function is not accessible from outside the function. PHP works differently from this in that the scope inside of any function is completely separate from the global scope and the function will not automatically have access to variables defined globally. The scope barrier exists in both directions in PHP where it only exists in one direction in JavaScript. The exceptions to this where the globals in PHP can be accessed from anywhere the way they can in JavaScript are called super globals.

The super globals can be broken up into several separate groups depending on where the variable values come from and whether you should be accessing them or not. Some of the super globals exist for historical reasons and would never be used in modern coding.

The two super globals $_POST and $_GET provide access to all of the values passed to the script as a part of the request from the previous page. The $_GET array contains all of the values passed in the query string on the end of the URL used to call the page when you don't expect the page to be updating anything and the $_POST array contains all of the values passed from a form that used method="POST" where the script is expected to perform updates of some sort. You can also use a form with method="GET" to load the $_GET array.

The $_FILES super global is also passed from forms. With these the form will contain one input field with type="File" for each file that is to be passed. This array differs from the previous two in that it is automatically a two dimensional array as there are multiple fields passed for each file.

The $_COOKIE and $_SESSION super globals provide a way to pass information between web pages. How they differ is that the $_COOKIE information is passed to the browser each time so that it can be stored on the local computer to retain the information between different browser sessions whereas the $_SESSION super global is held entirely on the server and only exists for the duration of the current browser session. To be able to access the $_SESSION super global the script must do a session start call as the very first thing in the script. When sessions are used the session identifier to link the entire session together will need to be passed between the pages in either the $_COOKIE or $_GET super global.

The super globals we have considered so far are all arrays where the content can be determined by the script author. You decide what fields or files you want to pass between web pages using the various different methods associated with those super globals. The $_SERVER and $_ENV super globals differ from these in that the particular variables passed via these two super global arrays is predefined. The $_ENV super global provides access to all of the fields available for the local environment (which you can also access via getenv and setenv - neither of which provides global access) and the $_SERVER super global provides access to server defined values that you can read but which are updated by the server itself.

The remaining two super globals are the two that exist for historical reasons and which have no place in modern PHP code. The $_REQUEST super global provides access to the content of the $_GET, $_POST and $_COOKIES super globals in a single array so that you cannot tell where the value came from. The $GLOBALS super global provides a way to access the various globally defined variables from other scopes where they would not ordinarily be accessible. A better alternative to using $GLOBALS is to do a 'global' declaration at the start of the scope to define which global variables the function should have access to - although even doing this is poor practice and indicates that your code could be improved.


This article written by Stephen Chapman, Felgall Pty Ltd.

go to top

FaceBook Follow
Twitter Follow