Superglobals are built-in variables that are always available in all scopes. They were added to PHP 4.1.0.
Some of PHP’s predefined variables are “superglobals,” which means that you can always use them, no matter what their scope is. You can use them from any function, class, or file without doing anything special.
We’ll learn about PHP’s superglobals.
These are array variables in PHP that are set up in a special way that makes it easy to find out about a request or its context. The superglobals can be used anywhere in your script. Any function, class, or file can use these variables without having to do anything special, like declare a global variable or something. In an application, they are mostly used to store information and get it from one page to another, etc.
These things are PHP’s superglobal variables:
- $GLOBALS
- $_SERVER
- $_REQUEST
- $_POST
- $_GET
- $_FILES
- $_ENV
- $_COOKIE
- $_SESSION
Now, let’s learn more about some of these superglobals:
PHP $GLOBALS
$GLOBALS is a PHP super global variable that lets global variables be accessed from anywhere in a PHP script (also from within functions or methods).
All global variables in PHP are kept in a list called $GLOBALS[index]. The name of the variable is written in the index.
Below is an example of how to use the super global variable $GLOBALS:
Example
<!DOCTYPE html>
<html>
<body><?php
$a = 75;
$b = 15;function addition() {
$GLOBALS[‘c’] = $GLOBALS[‘a’] + $GLOBALS[‘b’];
}addition();
echo $c;
?></body>
</html>
Output
90
In the above example, as c is a variable within the $GLOBALS array, it is also accessible outside of the function!
PHP $_SERVER
PHP has a super global variable called $_SERVER that stores information about headers, paths, and script locations.
In the example below, you can see how to use some of $_SERVER’s parts:
We used the $_SERVER elements in the code above to get some information. We get the name of the file that is being worked on right now by using the “PHP SELF” element. Then, we get the name of the server being used now by using the “SERVER NAME” element. Then, “HTTP HOST” gives us the name of the host.
Example
<!DOCTYPE html>
<html>
<body><?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’];
?></body>
</html>
Output
/example/superglobals.php
15.195.29.40
15.195.29.40
https://coderazaa.com/showphp.php?filename=demo_global_server
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36
/example/Superglobals .php
Element/Code | Description |
---|---|
$_SERVER[‘PHP_SELF’] | Returns the filename of the script that is currently running. |
$_SERVER[‘GATEWAY_INTERFACE’] | Returns the Common Gateway Interface (CGI) version that the server is using. |
$_SERVER[‘SERVER_ADDR’] | Returns the host server’s IP address. |
$_SERVER[‘SERVER_NAME’] | Returns the host server’s name (such as www.coderazaa.com) |
$_SERVER[‘SERVER_SOFTWARE’] | Returns the server ID string (for example, Apache/2.2.24) |
$_SERVER[‘SERVER_PROTOCOL’] | Returns the name and version number of the information protocol.(such as HTTP/1.1) |
$_SERVER[‘REQUEST_METHOD’] | Returns the method that was used to get to the page (such as POST) |
$_SERVER[‘REQUEST_TIME’] | Returns the date and time that the request was sent(such as 1377687496) |
$_SERVER[‘QUERY_STRING’] | Returns the query string if the page is accessed via a query string |
$_SERVER[‘HTTP_ACCEPT’] | Returns back the current request’s Accept header. |
$_SERVER[‘HTTP_ACCEPT_CHARSET’] | Returns the current request’s Accept-Charset header (such as utf-8,ISO-8859-1) |
$_SERVER[‘HTTP_HOST’] | Returns back the current request’s Host header. |
$_SERVER[‘HTTP_REFERER’] | Gives the full address of the current page (not reliable because not all user agents support it) |
$_SERVER[‘HTTPS’] | Is the script asked about using a safe HTTP protocol? |
$_SERVER[‘REMOTE_ADDR’] | Returns the user’s IP address from where the current page is being viewed. |
$_SERVER[‘REMOTE_HOST’] | Returns the name of the Host where the current page is being viewed. |
$_SERVER[‘REMOTE_PORT’] | Returns the port that the user’s computer is using to talk to the web server. |
$_SERVER[‘SCRIPT_FILENAME’] | Returns the full name of the script that is currently running. |
$_SERVER[‘SERVER_ADMIN’] | Returns the value that was given to the web server’s SERVER_ADMIN directive. configuration file (it will be the value of your script runs on a virtual host). (For example, example@coderazaa.com is a virtual host.) |
$_SERVER[‘SERVER_PORT’] | Returns the port that the web server is using to talk to the server (such as 80) |
$_SERVER[‘SERVER_SIGNATURE’] | Returns the server version and virtual hostname that is added to pages that are made by the server. |
$_SERVER[‘PATH_TRANSLATED’] | Returns the path to the current script based on the file system. |
$_SERVER[‘SCRIPT_NAME’] | Returns the current script’s path. |
$_SERVER[‘SCRIPT_URI’] | Gives you the URI of the page you’re on. |
PHP $_REQUEST
PHP $_REQUEST is a super global variable that is used to get information from an HTML form after it has been submitted.
In the example below, you can see a form with a text box and a button that says “Submit.” When a user clicks “Submit” on the form, the data is sent to the file named in the action attribute of the form> tag. In this case, we tell the computer to use this file to process form data. If you want to process form data with a different PHP file, replace that with the name of the file you want to use. Then, we can get the value of the input field by using the super global variable $_REQUEST:
Example
<!DOCTYPE html>
<html>
<body><form method=”post” action=”<?php echo $_SERVER[‘PHP_SELF’];?>”>
Name: <input type=”text” name=”name”>
<input type=”submit”>
</form><?php
if ($_SERVER[“REQUEST_METHOD”] == “POST”) {
// collect value of input field
$name = htmlspecialchars($_REQUEST[‘name’]);
if (empty($name)) {
echo “Name is empty”;
} else {
echo $name;
}
}
?></body>
</html>
Output
PHP $_POST
PHP $_POST is a super global PHP variable that is used to collect form data after an HTML form with method=”post” has been submitted. A lot of variables are also sent through $_POST.
In the example below, you can see a form with a text box and a button that says “Submit.” When a user clicks “Submit” on the form, the data is sent to the file named in the action attribute of the form> tag. In this case, we process form data by pointing to the file itself. If you want to process form data with a different PHP file, replace that with the name of the file you want to use. Then, we can get the value of the input field by using the super global variable $_POST:
The code is shown in the example below:
Example
<!DOCTYPE html>
<html>
<body><form method=”post” action=”<?php echo $_SERVER[‘PHP_SELF’];?>”>
Name: <input type=”text” name=”name”>
<input type=”submit”>
</form><?php
if ($_SERVER[“REQUEST_METHOD”] == “POST”) {
// collect value of input field
$name = htmlspecialchars($_POST[‘name’]);
if (empty($name)) {
echo “Name is empty”;
} else {
echo $name;
}
}
?></body>
</html>
Output
PHP $_GET
PHP $_GET is a super global variable in PHP that is used to collect form data after an HTML form with method=”get” has been submitted.
$_GET can also get information from the URL.
Let’s say we have an HTML page with a link that has parameters:
When a user clicks on the link “Test $GET,” the parameters “subject” and “web” are sent to “test get.php.” You can then use the $_GET variable to get the values of “subject” and “web” in “test get.php.”
The code is shown in the example below:
Example
<!DOCTYPE html>
<html>
<body><form method=”get” action=”<?php echo $_SERVER[‘PHP_SELF’];?>”>
Name: <input type=”text” name=”name”>
<input type=”submit”>
</form><?php
if ($_SERVER[“REQUEST_METHOD”] == “GET”) {
// collect value of input field
$name = htmlspecialchars($_GET[‘name’]);
if (empty($name)) {
echo “Name is empty”;
} else {
echo $name;
}
}
?></body>
</html>