This PHP Superglobals tutorial helps you learn the concept of superglobal variables.
Superglobals are some predefined variables in PHP. They are always accessible, irrespective of scope. These can be accessed by you from any function, class, or file without doing anything special.
Following are the PHP superglobal variables:
This PHP superglobal variable is used for accessing global variables from anywhere in the PHP script (even from within functions or methods).
All global variables are stored by PHP in an array called $GLOBALS[index]. The name of the variable is held by the index.
Example:
<?php
$x = 75;
$y = 25;
function addition() {
$GLOBALS['z'] = $GLOBALS['x'] + $GLOBALS['y'];
}
addition();
echo $z;
?>
Output:
100
Note: In the above-mentioned example, since z is a variable present within the $GLOBALS array, you can access it from outside the function!
This PHP superglobal variable holds information about headers, paths, and script locations.
Example:
<?php
echo $_SERVER['PHP_SELF'];
echo "<br>";
echo $_SERVER['SERVER_NAME'];
echo "<br>";
echo $_SERVER['HTTP_HOST'];
echo "<br>";
echo $_SERVER['HTTP_REFERER'];
echo "<br>";
echo $_SERVER['HTTP_USER_AGENT'];
echo "<br>";
echo $_SERVER['SCRIPT_NAME'];
?>
Output:
This will depends on your server
The table given below lists the most important elements/code that can go in $_SERVER:
Element/Code | Description |
---|---|
$_SERVER['PHP_SELF'] | It will return the filename of the currently executing script |
$_SERVER['GATEWAY_INTERFACE'] | It will return the version of the Common Gateway Interface (CGI) being used by the server. |
$_SERVER['SERVER_ADDR'] | It will return the host server's IP address |
$_SERVER['SERVER_NAME'] | It returns the host server's name |
$_SERVER['SERVER_SOFTWARE'] | The server identification string (such as Apache/2.2.24) is returned by it |
$_SERVER['SERVER_PROTOCOL'] | It will return the information protocol's name and revision (such as HTTP/1.1) |
$_SERVER['REQUEST_METHOD'] | It will return the request method used for accessing the page (such as POST) |
$_SERVER['REQUEST_TIME'] | It will return the timestamp of the start of the request (such as 1377687496) |
$_SERVER['QUERY_STRING'] | It will return the query string if the page is accessed via a query string |
$_SERVER['HTTP_ACCEPT'] | It will return the Accept header from the current request |
$_SERVER['HTTP_ACCEPT_CHARSET'] | It will return the Accept_Charset header from the current request (such as utf-8,ISO-8859-1) |
$_SERVER['HTTP_HOST'] | It will return the Host header from the current request |
$_SERVER['HTTP_REFERER'] | It returns the current page's complete URL (unreliable because not all user-agents support it) |
$_SERVER['HTTPS'] | It is the script queried via a secure HTTP protocol |
$_SERVER['REMOTE_ADDR'] | It will return the IP address from where the user is viewing the current page |
$_SERVER['REMOTE_HOST'] | It will return the Hostname from where the user is viewing the current page |
$_SERVER['REMOTE_PORT'] | It will return the port that the user's machine is using to |
$_SERVER['SCRIPT_FILENAME'] | It returns the currently executing script's absolute pathname |
$_SERVER['SERVER_ADMIN'] | It returns the value given to the SERVER_ADMIN directive in the web server configuration file (in case your script runs on a virtual host, then it will be the value set for that virtual host) (like [email protected]) |
$_SERVER['SERVER_PORT'] | It will return the port on the server machine the web server is using for communication (such as 80) |
$_SERVER['SERVER_SIGNATURE'] | It will return the server version and virtual host name, which are added to server-generated pages |
$_SERVER['PATH_TRANSLATED'] | It will return the file system based path to the current script |
$_SERVER['SCRIPT_NAME'] | It returns the current script's path |
$_SERVER['SCRIPT_URI'] | It will return the URI of the current page |
This PHP superglobal variable is used for collecting data after submitting an HTML form.
The below-mentioned example depicts a form with an input field and a submit button. When the data is submitted by a user by clicking on "Submit", it will send the form data to the file specified in the <form> tag's action attribute. We point to this file itself to process form data in the example here. In case you want to use a different PHP file for processing form data, you can replace it with your choice of filename. Then, the superglobal variable $_REQUEST can be used to collect the value of the input field.
Example:
<html>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
Name: <input type="text" name="fname">
<input type="submit">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// collect value of input field
$name = $_REQUEST['fname'];
if (empty($name)) {
echo "Name is empty";
} else {
echo $name;
}
}
?>
</body>
</html>
This PHP superglobal variable collects form data after submitting an HTML form with method="post". It is also used to pass variables.
The below-mentioned example depicts a form with an input field and a submit button. When the data is submitted by a user by clicking on "Submit", it sends the form data to the file specified in the <form> tag's action attribute. We point to this file itself to process form data in the example here. In case you want to use a different PHP file for processing form data, you can replace it with your choice of filename. Then, the superglobal variable $_POST can be used to collect the value of the input field.
<html>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
Name: <input type="text" name="fname">
<input type="submit">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// collect value of input field
$name = $_POST['fname'];
if (empty($name)) {
echo "Name is empty";
} else {
echo $name;
}
}
?>
</body>
</html>
This PHP superglobal variable collects form data after submitting an HTML form with the method="get".
It also collects data sent in the URL.
Now, assume you have an HTML page containing a hyperlink with parameters:
<html>
<body>
<a href="test_get.php?subject=PHP&web=developerstutorial.com">Test $GET</a>
</body>
</html>
When the link "Test $GET" is clicked by a user, it will send the parameters "subject" and "web" to "test_get.php", and their values in "test_get.php" can be accessed by you with $_GET.
The below-mentioned example shows the code in "test_get.php":
<html>
<body>
<?php
echo "Study " . $_GET['subject'] . " at " . $_GET['web'];
?>
</body>
</html>