PHP TUTORIALPHP TutorialPHP InstallationPHP Hello WorldPHP Basic SyntaxPHP CommentsPHP VariablesPHP Variable ScopePHP ConstantsPHP StringsPHP OutputPHP Data TypesPHP Type CastingPHP OperatorsPHP ConditionalsPHP Shorthand ConditionalsPHP LoopsPHP Loop Control StructuresPHP FunctionsPHP String FunctionsPHP ArraysPHP Superglobal VariablesPHP in HTMLPHP AdvancedPHP Include and RequirePHP 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 OOP IntroductionPHP OOP ClassesPHP OOP PropertiesPHP OOP ObjectsPHP OOP MethodsPHP OOP $this KeywordPHP OOP Constructors and DestructorsPHP OOP VisibilityPHP OOP InheritancePHP OOP Abstract Classes and MethodsPHP OOP InterfacesPHP OOP TraitsPHP OOP ConstantsPHP OOP StaticPHP OOP NamespacesPHP OOP Autoloading

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.

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

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 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).

$GLOBALS

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

PHP $GLOBALS Example


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

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

websiteName(); // outputs HyvorDeveloper

Run Example ››

$_SERVER

$_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.

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.

PHP $_SERVER Example


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

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.

Facebook Twitter