PHP provides 9 Super-globals, which are accessible, anywhere in the entire PHP script. Each Super-global is a built-in variable, and has its special meaning and they are
- $GLOBALS
- $_SERVER
- $_REQUEST
- $_POST
- $_GET
- $_FILES
- $_ENV
- $_COOKIE
- $_SESSION
All superglobals are associative array holds information (data) as key-value pair.
$GLOBALS
This super-global, $GLOBALS is used to access global variables in PHP script. It is an associative array, which holds global variable and its value as key-value pair.
<?php
$a = 20;
function getValue(){
echo $GLOBALS['a'];
}
getValue();
?>
$_SERVER
$_SERVER is a super-global, provides headers, paths, and script locations information. This information is created by web server.
Example
<?php
echo $_SERVER['SERVER_NAME'];
?>
$_SERVER[‘PHP_SELF’] | Returns currently executing script filename |
$_SERVER[‘GATEWAY_INTERFACE’] | Server’s Common Gateway Interface (CGI) version currently using |
$_SERVER[‘SERVER_ADDR’] | Returns Host Server IP address |
$_SERVER[‘SERVER_NAME’] | Returns Host Server name (yourshala.com) |
$_SERVER[‘SERVER_SOFTWARE’] | Returns Server identification string |
$_SERVER[‘SERVER_PROTOCOL’] | Returns information protocol name and revision |
$_SERVER[‘REQUEST_METHOD’] | Page request method i.e. POST or GET, used to access the page |
$_SERVER[‘REQUEST_TIME’] | Starting time stamp of page request. |
$_SERVER[‘REQUEST_TIME_FLOAT’] | Starting time stamp with microsecond precision, of page request. |
$_SERVER[‘QUERY_STRING’] | Returns query string. |
$_SERVER[‘HTTP_ACCEPT’] | Returns current request Accept header |
$_SERVER[‘HTTP_ACCEPT_CHARSET’] | Returns Accept_Charset header of current request |
$_SERVER[‘HTTP_HOST’] | Returns current request, Host header |
$_SERVER[‘HTTP_REFERER’] | Returns current page complete or full URL |
$_SERVER[‘HTTPS’] | A script queried using secure HTTP protocol |
$_SERVER[‘REMOTE_ADDR’] | Returns User’s IP address, from current page is being viewed. |
$_SERVER[‘REMOTE_HOST’] | Returns User’s Host address, from current page is being viewed. |
$_SERVER[‘REMOTE_PORT’] | Returns User’s Port, from user is connected to web server. |
$_SERVER[‘SCRIPT_FILENAME’] | Returns currently executing php script absolute pathname. |
$_SERVER[‘SERVER_ADMIN’] | Gives value of SERVER_ADMIN directive of web server configuration file. |
$_SERVER[‘SERVER_PORT’] | Web server port number used for communication |
$_SERVER[‘SERVER_SIGNATURE’] | Returns server version and virtual host name, added to server-generated pages |
$_SERVER[‘PATH_TRANSLATED’] | Returns current script file system based path. |
$_SERVER[‘SCRIPT_NAME’] | Returns current script path |
$_SERVER[‘SCRIPT_URI’] | Returns current page URI |
$_SERVER[‘REMOTE_USER’] | Returns authenticated user. |
$_SERVER[‘REQUEST_URI’] | URI used to access web page. e.g. /index.html |
$_SERVER[‘DOCUMENT_ROOT’] | Returns current executing script document root directory. |
$_REQUEST
This $_REQUEST superglobal contains all $_GET, $_POST and $_COOKIE values or data.
<form method="get">
<input type="text" id="myname" name="myname"/>
<input type="submit" value="OK" />
</form>
<?php
if(!empty($_REQUEST["myname"]))
echo $_REQUEST["myname"];
?>
$_GET
Allows to get User submitted form data using HTTP GET REQUEST method in the form of query string.
<form method="get">
<input type="text" id="myname" name="myname"/>
<input type="submit" value="OK" />
</form>
<?php
if(!empty($_GET["myname"]))
echo $_GET["myname"];
?>
$_POST
Allows to get User submitted form data using HTTP POST REQUEST method.
<form method="post">
<input type="text" id="myname" name="myname"/>
<input type="submit" value="OK" />
</form>
<?php
if(!empty($_POST["myname"]))
echo $_POST["myname"];
?>
$_FILES
Used to Upload file using HTTP POST REQUEST method
$_SESSION
This $_SESSION superglobal holds data and can be accessed in multiple web pages.
Page 1
$_SESSION["username"] = "Vasu";
Page 2
echo $_SESSION["username"];
$_ENV
It provides information of current Environment in which PHP script is executing.
<?php
echo 'username is ' .$_ENV["USER"] . '!';
?>
$_COOKIES
Used to hold and get data via HTTP Cookies.
$_COOKIE["name"] = "Hello";
echo $_COOKIE["name"];