Learn PHP
PHP techniques
PHP basics
PHP Tutorial
PHP History
Command Line PHP
PHP Syntax
PHP Variables
PHP Superglobals
PHP Strings

PHP Superglobals

Debugging Tip

In order to be able to debug many PHP scripts, you often need to confirm exactly what data a PHP page receives or can access. Towards this end, printing the values of a Superglobal in PHP can be quite useful.

Server Variations

Different than $_GET, $_POST, $_COOKIE, $_REQUEST, $_SESSION, and $_FILES, the values of the $_SERVER and $_ENV Superglobals will differ greatly from one server to the next. Everything from the Web server being used (Apache, IIS, etc.) to the operating system to the user that PHP runs as will play a role. Some of the $_SERVER elements may not be reliable, such as $_SERVER['HTTP_REFERER'], and practically all of $_SERVER is meaningless when running PHP from the command line.

Altering the PHP Superglobals

You cannot define your own Superglobal variables in PHP. You can, although this is not commonly necessary, add values to $GLOBALS (or even $_GET, $_POST, and the others). The Superglobals essentially replace older versions of each array, which are now deprecated. For example, it is preferable to use $_GET instead of $HTTP_GET_VARS and $_COOKIE instead of $HTTP_COOKIE_VARS. As of PHP 5, the older versions of these variables, for example, $HTTP_GET_VARS, can be disabled. Note that technically the new and older versions of each variable (e.g., $_GET and $HTTP_GET_VARS) are separate variables that initially store the same values. Manipulations of one array will not affect the other:
// Assume this page is accessed as page.php?var=value; 
$_GET['var'] = 'new'; 
echo $_GET['var']; // Prints "new". 
echo $HTTP_GET_VARS['var']; // Prints "value".
This same relationship exists between $_GET, $_POST, $_COOKIE, and $_REQUEST. Changes made to one Superglobal array within a script will not affect the other.
// Assume this page is accessed as page.php?var=value; 
$_GET['var'] = 'new'; 
echo $_GET['var']; // Prints "new". 
echo $_REQUEST['var']; // Prints "value".

Article continues:  <<previous page

 Download NuSphere PHP IDE

Download a free trial of the fast PHP EDitor and robust Integrated Development Environment for PHP.

 Buy NuSphere PhpED® now

Best PHP Editor and complete PHP IDE.
NuSphere PhpED 19.0 is available from our online store front.

 Special Team4 Offer

Get 4 copies of PhpED for the price of 3!

Optimum solution for development teams.
PhpED 19.0 Team4
Need more than 4 licenses? Contact Us for more quantity discounts, please use "Ordering/Payment issue" subject on the form.
"To be honest its bloody awesome, I have looked at loads of PHP editors and this is THE only one that actual works straight out of the box!!! Brilliant, well done."
Andrew Breward,
Director of Technology