PHP Super Global Variables
Some predefined variables in PHP are "superglobals" ,which means they are always available, regardless of scope, and may be accessed from any function, class, or file without any particular effort.
PHP superglobals in PHP variables :
- $GLOBALS
- $_SERVER
- $_REQUEST
- $_POST
- $_GET
- $_FILES
- $_ENV
- $_COOKIE
- $_SESSION
PHP $GLOBALS Variable
$GLOBALS
is a super global PHP variable used to access global variables in PHP scripts from any place (also from within functions or methods).
PHP keeps track of all global variables in an array named $GLOBALS[index]
. The variable name is included in the index.
Example :- The following example demonstrates how to utilize a super global variable.
Output :-
Since $z is a variable within the $GLOBALS
array, it is also available from outside the function in the example above!
PHP $_SERVER Variable
Super global variables are built-in variables, which always may be found in every scope.
$_SERVER
is an overall PHP variable that contains Headers, Paths, and Locations of the script information.
Example :- The following example demonstrates how to take advantage of some of the elements in $_SERVER
:
";
echo $_SERVER['SERVER_NAME'];
echo "
";
echo $_SERVER['HTTP_HOST'];
echo "
";
echo $_SERVER['HTTP_REFERER'];
echo "
";
echo $_SERVER['HTTP_USER_AGENT'];
echo "
";
echo $_SERVER['SCRIPT_NAME'];
?>
Output :-
35.194.26.41
35.194.26.41
https://tryphp.w3schools.com/showphp.php?filename=demo_global_server
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36
/demo/demo_global_server.php
The following table lists the most important elements that can go inside $_SERVER
:
Element/Code | Description |
---|---|
$_SERVER['PHP_SELF'] | The current script's filename is returned. |
$_SERVER['GATEWAY_INTERFACE'] server is | The Common Gateway Interface (CGI) version the user is using is returned. |
$_SERVER['SERVER_ADDR'] | The host server's IP address is returned. |
$_SERVER['SERVER_NAME'] | The host server's name is returned (such as www.simmanchith.com) |
$_SERVER['SERVER_SOFTWARE'] | The server identification string (e.g., Apache/2.2.24) is returned. |
$_SERVER['SERVER_PROTOCOL'] | The information protocol's name and revision (for example, HTTP/1.1) are returned. |
$_SERVER['REQUEST_METHOD'] | The page's request method is returned (such as POST). |
$_SERVER['REQUEST_TIME'] | The timestamp of the request's start is returned (such as 1377687496). |
$_SERVER['QUERY_STRING'] | If the page was reached through a query string, the query string is returned. |
$_SERVER['HTTP_ACCEPT'] | The Accept header from the current request is returned. |
$_SERVER['HTTP_ACCEPT_CHARSET'] | The Accept Charset header from the current request (for example, utf-8, ISO-8859-1) is returned. |
$_SERVER['HTTP_HOST'] | The current request's Host header is returned. |
$_SERVER['HTTP_REFERER'] | Returns the whole URL (not trustworthy, since not all users support it) of the current page. |
$_SERVER['HTTPS'] | Is the script accessed using an encrypted HTTP protocol? |
$_SERVER['REMOTE_ADDR'] | The IP address of the user who is now browsing the page is returned. |
$_SERVER['REMOTE_HOST'] | The name of the host from which the user is seeing the current page is returned. |
$_SERVER['REMOTE_PORT'] | The port used to communicate with the web server on the user's system is returned. |
$_SERVER['SCRIPT_FILENAME'] | The absolute pathname of the currently running script is returned. |
$_SERVER['SERVER_ADMIN'] | Returns the value specified in a web site configuration file to the SERVER ADMIN directive (if you have your script running on a virtual Host, it will be a virtual host value) (such as somebody@w3schools.com) |
$_SERVER['SERVER_SIGNATURE'] | The server version and virtual host name that appear on server-generated pages are returned. |
$_SERVER['PATH_TRANSLATED'] | The current script's file system-based path is returned. |
$_SERVER['SCRIPT_NAME'] | The current script's path is returned. |
$_SERVER['SCRIPT_URI'] | The current page's URI is returned. |
Related Links
PHP $_GET Variable
The $_GET
is a global variable that collects form data with method="get" after the form submission.
$_GET
can also collect data sent in the URL.
Assume we have an HTML page that contains a hyperlink with parameters :
Test $GET
When a user hits the link "Test $GET", the parameters "name" and "web" are passed to "demo.php",
and their values may then be accessed using $_GET
in "demo.php." :
Example :- The example below shows the code in "test_get.php" :
Output :-
PHP $_POST Variable
PHP $_POST
is a global variable that collects data once an HTML form is submitted.
PHP$_POST
is a PHP super global variable used after submission of the method="post" in the form of data collection. Uses $_POST to get variables are extensively utilized.
Related Links
PHP $_REQUEST Variable
Super global variables are built-in variables, which always may be found in every scope.
PHP $_REQUEST
is a global variable that collects data once an HTML form is submitted.