PHP Tutorial
PHP Superglobals
PHP superglobals are predefined variables that are available in all scopes of the script. They can be used to represent data coming from URLs, HTML forms, Cookies, Sessions. etc. They were firstly introduced in PHP 4.1.
The PHP superglobals are:
$_GET$_POST$_SERVER$_REQUEST$_SESSION$_COOKIE$_ENV$_FILES$GLOBALS
$_GET
The PHP $_GET is a superglobal variable that is used to submit an HTML form with the get method. It is an array of variables passed via the URL parameters. It is populated for all requests with a query string.
None
The above example is a simple HTML form that submits a form with the get method whose values can be accessed via the $_GET variable.
$_POST
The PHP $_POST is a superglobal variable that is used to submit an HTML form with the post method. Unlike the $_GET superglobal, the information is not stored in the URL.
$_POST ExampleThe above example is a simple HTML form that submits a form with the post method whose values can be accessed via the $_POST variable.
$_SERVER
The PHP $_SERVER is a superglobal that stores information about the headers, paths and location of a PHP script. This variable holds an array of values which are created by the web server. The following include some of the available entries:
- $_SERVER['SERVER_NAME'] - This holds the name of the server which the site was hosted.
- $_SERVER['PHP_SELF'] - This holds the name of the filename of the currently executed PHP script.
- $_SERVER['PHP_HOST'] - This holds the header of the host of the current request if it has any.
- $_SERVER['SCRIPT_NAME'] - This file path of the currently executed script.
- $_SERVER['REMOTE_ADDR'] - This holds the IP address from which the user is currently viewing the page.
$_SERVER ExampleThe above example uses the $_SERVER variable to output the name of the server, the filepath and filename of the current PHP script.
$_REQUEST
The PHP $_REQUEST is a superglobal that contains the data of $_GET, $_POST and $_COOKIE by default.
$_REQUEST ExampleThe above example shows how to use the $_REQUEST variable to get data from both the get and post request.