PHP TUTORIALPHP TutorialPHP InstallationPHP Hello WorldPHP Basic SyntaxPHP CommentsPHP VariablesPHP Variable ScopePHP ConstantsPHP OutputPHP Data TypesPHP Type CastingPHP OperatorsPHP ConditionalsPHP Shorthand ConditionalsPHP LoopsPHP Loop Control StructuresPHP FunctionsPHP StringsPHP ArraysPHP Superglobal VariablesPHP in HTMLPHP RegexRegex IntroductionRegex PCRE SyntaxPHP PREG FunctionsPHP FormsPHP Forms IntroductionPHP Forms CreatingPHP Forms SecurityPHP Forms ValidationPHP Forms Required InputsPHP Forms StickyPHP Forms Advanced ValidationPHP Forms FinishingPHP OOPPHP Object Oriented ProgrammingPHP OOP ClassesPHP OOP PropertiesPHP OOP ObjectsPHP OOP MethodsPHP OOP $this KeywordPHP OOP Constructors and DestructorsPHP OOP VisibilityPHP OOP Inheritance

PHP Superglobal Variables

Superglobals are built-in variables that are always available in all the scopes

We used $GLOBALS array to access globals variables inside a function in the variables chapter. We could use $GLOBALS inside a function directly because it is a superglobal variable.

Important: All superglobal variables are available everywhere in the script.

PHP has 9 superglobals. All of them are indexed or associative arrays which contain a specific set of data.

Tip: Note that all the superglobals has an _ (underscore) after the $ (dollar sign) except $GLOBALS.

PHP has a lot of elements in superglobal arrays. It's really hard to memorize all of them at once. Just understand what each superglobal variable is about. When you finish this tutorial, you will learn about the elements of superglobal arrays by practice, not by studying.
Superglobal Variable Description
$GLOBALS Stores all the global scope variables.
$_SERVER Stores information about the server and execution environment.
$_GET Stores HTTP GET variables.
$_POST Stores HTTP POST variables.
$_FILES Stores HTTP file upload variables.
$_ENV Stores environment variables.
$_COOKIE Stores HTTP Cookies.
$_SESSION Stores session variables.
$_REQUEST Stores request variables ($_GET, $_POST and $_COOKIE variables).


$GLOBALS is a superglobal array which contains all the global scope variables.


$x = 'Hyvor';
$y = 'Developer';

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

websiteName(); // outputs HyvorDeveloper

Run Example ››


$_SERVER is a very useful and huge array which holds the data about the currently executing script, network addresses, paths, locations, etc.

The PHP manual has a full list of available elements in the $_SERVER superglobal array. But, we will not need all of them. We will be discussing the commonly used elements in this tutorial. The below table will list the elements with a description of its value.

Element Description
PHP_SELF The filename of the currently executing script, relative to the document root.
DOCUMENT_ROOT The document root directory of the server.
SERVER_ADDR The IP address of the server.
SERVER_NAME The name of the server. (For instance,
REQUEST_METHOD The request method that the page was requested with. (Ex: POST, GET, HEAD, etc.)
REQUEST_TIME The timestamp of the start of the request.
HTTP_USER_AGENT The User-Agent header sent by the browser. (This is used to detect OS, Browser, etc.)
REMOTE_ADDR The IP address of the current user.

PHP $_SERVER Example

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>';

We will learn about $_GET, $_POST and $_REQUEST arrays in the forms chapter.

$_FILES, $_COOKIES, and $_SESSION variables will be described in the later chapters of this tutorial.