In this article you are going to learn about the superglobal variables in php. Well, superglobal variables or simply superglobals are the predefined built-in variables that comes with the php versions.
They were first appended in the php4 version. Since then they have been continuously helping the php programmers to achieve the desired results. They form an integral part of the variables in php programming language. Join w3training School php training in gurgaon and learn how usage of superglobal php variables.
The purpose of introducing superglobal variables in php is to increase the scope of variables inside a php script. In other words, they should be accessible throughout the whole php script.
Some of the superglobal variables in php are listed below.
- $GLOBALS
- $_SERVER
- $_REQUEST
- $_FILES
- $_COOKIE
- $_ENV
- $_SESSION
- $_POST
- $_GET
$GLOBALS VARIABLE
This superglobal variable is an associative array used for accessing the variables from within the functions, methods or from anywhere else in the whole php script.
Basic Syntax : $GLOBALS[‘variable’];

Example : An example of $GLOBALS superglobal variable is shown below.

Output : You will get the following output of the above php script.
$_SERVER VARIABLE
This superglobal variable is an array which is used to get information about headers, paths, script locations, IP address of the client, host server information etc. All the information we get by the execution of the php script using $_SERVER is entirely dependent on the web server. It may or may not provide the desired information to the user.
Basic Syntax : $_SERVER[‘element’];
Following is a list of elements that a $_SERVER variable comprises.
- PHP_SELF
- SERVER_ADDR
- SERVER_SOFTWARE
- QUERY_STRING
- HTTPS
- REMOTE_ADDR
- REMOTE_HOST
- SERVER_PORT
- REMOTE_PORT
- HTTP_HOST
- SERVER_PROTOCOL
- GATEWAY_INTERFACE
- SCRIPT_URI
- SERVER_SIGNATURE
- SCRIPT_FILENAME
- HTTP_ACCEPT_CHARSET
- AUTH_TYPE
- REDIRECT_REMOTE_USER

Example : An example of $_SERVER superglobal variable is shown below.

Output : The following output will be generated using the above php code.
$_REQUEST VARIABLE
This superglobal variable is an associative array and is used to make request to the web server in order to get details of an HTML form. It includes the contents of three superglobals namely $_GET, $_POST and $_COOKIE.
Basic Syntax : $_REQUEST[‘xyz’];

Example : An example of $_REQUEST superglobal variable is shown below.

Output : The output of the above php script using $_REQUEST superglobal variable is shown below.
$_FILES VARIABLE
This superglobal variable represents an associative array and is used to upload files to the php scripts.
Basic Syntax : $_FILES[‘file’];

Example : An example of $_FILES superglobal variable is shown below.

Output : You will get the following output using $_FILES variable in the above php script.
$_COOKIE VARIABLE
It represents an associative array and is used to fetch user information sent by the web server. Whenever a user requests a page, a cookie is sent to the computer and to fetch the information of that cookie, we use $_COOKIE variable.
Basic Syntax : $_COOKIE[‘xyz’];

Example : An example of $_COOKIE superglobal variable is shown below.

Output : The following output will be generated using the above php code.
$_SESSION VARIABLE
This is also an associative array which is comprised of session variables that are available in all scope to the php script.
Basic Syntax : $_SESSION[‘xyz’];

Example : An example of $_SESSION superglobal variable is shown below.

Output : You will get the following output using $_SESSION variable in the above php script.
Above, we have provided you the information about superglobals in php with an example of each. If you liked this article, comment and give your opinion. We would definitely concern your thoughts for further citation and improvements. Stay tuned to our php blogs.