Introduction to PHP Superglobal Variables

Superglobal variables in PHP are foundational structures always accessible no matter what the scope. On a global scale, they can be used directly inside functions, as exhibited by the $GLOBALS array explored in previous discussions. This accessibility stems from their ‘superglobal’ status, translating to broad availability throughout the script.

If you’re interested in the Super Global Array in PHP, you may also like learning about namespaces in PHP, which provide a way to group related classes and functions, further streamlining and organizing your code.

PHP’s Nine Superglobals

PHP code snippet showing a function accessing a global variable

There are nine PHP superglobal variables, each essentially acting as an indexed or associative array holding specific data sets. These include:

  • $GLOBALS;
  • $_SERVER;
  • $_GET;
  • $_POST;
  • $_FILES;
  • $_ENV;
  • $_COOKIE;
  • $_SESSION;
  • $_REQUEST.

Notably, $GLOBALS is the only superglobal variable that doesn’t follow the _$ (underscore after the dollar sign) naming convention.

Each of these superglobals has a specific role in the PHP ecosystem, which may be somewhat overwhelming to remember initially. With practice, however, their functions become more intuitive.

Superglobal VariableDescription
$GLOBALSStores all the global scope variables.
$_SERVERStores information about the server and execution environment.
$_GETStores HTTP GET variables.
$_POSTStores HTTP POST variables.
$_FILESStores HTTP file upload variables.
$_ENVStores environment variables.
$_COOKIEStores HTTP Cookies.
$_SESSIONStores session variables.
$_REQUESTStores request variables ($_GET, $_POST and $_COOKIE variables).

An Overview of $GLOBALS

The $GLOBALS superglobal array serves as a repository for all globally scoped variables. Here’s an example to illustrate:

<?php
$x = 'Hyvor';
$y = 'Developer';

function websiteName() {
	echo $GLOBALS['x'], $GLOBALS['y'];
}

websiteName(); // outputs HyvorDeveloper

An Understanding of $_SERVER

Illustrated person with mobile application interface elements around them

The $_SERVER superglobal array is a comprehensive container of information regarding the currently running script, network addresses, paths, locations and more. The PHP manual offers a detailed inventory of available elements within the $_SERVER superglobal array. This guide will only touch on the commonly used ones.

ElementDescription
PHP_SELFThe filename of the currently executing script, relative to the document root.
DOCUMENT_ROOTThe document root directory of the server.
SERVER_ADDRThe IP address of the server.
SERVER_NAMEThe name of the server. (For instance, developer.hyvor.com)
REQUEST_METHODThe request method that the page was requested with. (Ex: POST, GET, HEAD, etc.)
REQUEST_TIMEThe timestamp of the start of the request.
HTTP_USER_AGENTThe User-Agent header sent by the browser. (This is used to detect OS, Browser, etc.)
REMOTE_ADDRThe IP address of the current user.

Example of usage:

<?php
echo $_SERVER['PHP_SELF'] . '<br>';
echo $_SERVER['DOCUMENT_ROOT'] . '<br>';
echo $_SERVER['SERVER_ADDR'] . '<br>';
echo $_SERVER['SERVER_NAME'] . '<br>';
echo $_SERVER['REQUEST_METHOD'] . '<br>';
echo $_SERVER['REQUEST_TIME'] . '<br>';
echo $_SERVER['HTTP_USER_AGENT'] . '<br>';
echo $_SERVER['REMOTE_ADDR'];

Conclusion

PHP superglobal arrays are undeniably powerful tools in web development. Their versatility and wide-scale accessibility allow developers to manage a plethora of functionalities, from accessing global variables to storing HTTP and server information. Mastering their use can significantly boost your PHP development skills, enabling you to create more dynamic and user-centric applications. Remember, it’s not about memorizing each superglobal, but understanding their purpose and how they can be manipulated to enhance your web application operations.