If you are an avid programmer, you often would have faced the problem of repeatedly defining new variables in each file. This is a very common problem, and to solve this, PHP has given us some predefined variables.
These predefined variables are frequently required across different files. PHP has these variables as PHP Superglobals. These superglobals solve this frustrating problem.
Let us see how it is used and how it solves the problem.
List of Superglobal Variables
PHP Superglobal variables are built-in variables always available in all scopes.
Some PHP variables are "superglobals," which means they are always available, regardless of scope, and can be accessed from any file, class, or function without doing anything special.
The PHP superglobal variables are:
1. $GLOBALS:
Description: An associative array containing references to all variables currently defined in the global scope of the script.
Usage Example: Accessing a global variable inside a function.
2. $_SERVER:
Description: An associative array containing information such as headers, paths, and script locations. The entries in this array are created by the web server.
Usage Example: Getting the server and execution environment information.
3. $_REQUEST:
Description: An associative array that by default contains the contents of $_GET, $_POST, and $_COOKIE. This array is useful for collecting data sent via HTTP request.
Usage Example: Accessing form data sent via GET or POST methods.
4. $_POST:
Description: An associative array of variables passed to the current script via the HTTP POST method. Often used to collect form data.
Usage Example: Accessing form data submitted via POST.
5. $_GET:
Description: An associative array of variables passed to the current script via the URL parameters (query string). Commonly used to pass data in URLs.
Usage Example: Accessing data sent in the URL.
6. $_FILES:
Description: An associative array of items uploaded to the current script via the HTTP POST method. Used to handle file uploads.
Usage Example: Handling file uploads.
7. $_ENV:
Description: An associative array of environment variables passed to the current script via the environment. Typically set by the server or the operating system.
Usage Example: Accessing environment variables.
8. $_COOKIE:
Description: An associative array of variables passed to the current script via HTTP cookies. Used to store data on the client's machine and retrieve it on subsequent requests.
Usage Example: Accessing cookie values.
9. $_SESSION:
Description: An associative array containing session variables available to the current script. Used to maintain data across different pages of a website.
Usage Example: Working with session data.
Later, we will see some of the superglobals in detail.
PHP $GLOBALS
$GLOBALS is a superglobal variable in PHP that allows you to access global variables anywhere in your script (also from within functions or methods).
All global variables are stored in an array called $GLOBALS[index] in PHP. The variable's name is stored in the index.
The following example demonstrates how to make use of the superglobal variable.
The essential elements that can be inside $_SERVER are listed in the table below:
Element/Code
Description
$_SERVER['GATEWAY_INTERFACE']
It returns the version of the Common Gateway Interface (CGI) the server is using.
$_SERVER['PHP_SELF']
It returns the filename of the currently executing script.
$_SERVER['SERVER_NAME']
It returns the name of the host server (such as www.codingninjas.com)
$_SERVER['SERVER_ADDR']
It returns the IP(Internet Protocol) address of the host server
$_SERVER['SERVER_PROTOCOL']
It returns the name and revision of the information protocol (such as HTTP/1.1)
$_SERVER['SERVER_SOFTWARE']
It returns the server identification string (such as Apache/2.2.24)
$_SERVER['REQUEST_TIME']
It returns the timestamp of the start of the request (such as 1377687496)
$_SERVER['REQUEST_METHOD']
It returns the request method used to access the page (such as POST)
$_SERVER['HTTP_ACCEPT']
It returns the Accept header from the current request.
$_SERVER['QUERY_STRING']
It returns the query string if the page is accessed via a query string.
$_SERVER['HTTP_HOST']
It returns the Host header from the current request.
$_SERVER['HTTP_ACCEPT_CHARSET']
It returns the Accept_Charset header from the current request (such as utf-8, ISO-8859-1)
$_SERVER['HTTPS']
It is the script queried through a secure HTTP protocol.
$_SERVER['HTTP_REFERER']
It returns the complete URL(Uniform Resource Locator) of the current page (not reliable because not all user-agents support it)
$_SERVER['REMOTE_HOST']
It returns the hostname from where the user is viewing the current page.
$_SERVER['REMOTE_ADDR']
It returns the IP address from where the user views the current page.
$_SERVER['SCRIPT_FILENAME']
It returns the absolute pathname of the currently executing script.
$_SERVER['REMOTE_PORT']
It returns the port used on the user's machine to communicate with the webserver.
$_SERVER['SERVER_PORT']
It returns the port on the server machine being used by the webserver for communication (such as 80)
$_SERVER['SERVER_ADMIN']
It returns the value specified in the SERVER ADMIN directive in the web server configuration file (if your script runs on a virtual host, the value specified for that virtual host) (for example, someone@codingninjas.com).
$_SERVER['PATH_TRANSLATED']
It returns the file system based path to the current script.
$_SERVER['SERVER_SIGNATURE']
It returns the server version and virtual hostname, which are added to server-generated pages.
$_SERVER['SCRIPT_URI']
It returns the URI of the current page
$_SERVER['SCRIPT_NAME']
It returns the path of the current script
PHP $_REQUEST
PHP $_REQUEST is a PHP superglobal variable used to collect data after submitting an HTML form.
A form with an input field and a submit button is shown in the example below. The form data is sent to the file provided in the action element of the form> tag when a user submits the data by clicking "Submit." For processing form data, we point to this file in our example. Replace that with the filename of your choice if you want to process form data with another PHP file. The input field's value can then be collected using the super global variable $_REQUEST.
Example
<html>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
FullName: <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>
Output
Coding Ninjas
PHP $_POST
PHP $_POST is a PHP superglobal variable which is used to collect form data after submitting an HTML form with method="post". $_POST is also widely used to pass variables.
The example below shows a form with an input field and a submit button. When a user submits the data by clicking on "Submit", the form data is sent to the file specified in the action attribute of the <form> tag. In this example, we point to the file for processing form data. If you wish to use another PHP file to process form data, replace that with the filename of your choice. Then, we can use the super global variable $_POST to collect the input field's value.
Example
<html>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
FullName: <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>
Output
Coding Ninjas
PHP $_GET
PHP $_GET is a PHP superglobal variable that collects form data once an HTML form with method="get" is submitted.
Data sent via the URL can also be collected with $_GET.
Assume we have an HTML page with the following parameters in a hyperlink:
The parameters "subject" and "web" are passed to "test get.php" when a user clicks on the link "Test $GET," and you may retrieve their values in "test get.php" with $_GET.
The code in "check_get.php" is shown in the example below:
PHP is an acronym for "PHP: Hypertext Preprocessor". It is basically an open-source scripting language.
What is a PHP superglobal variable?
PHP Superglobal variables are built-in variables that are always available in all scopes.
How can we find the filename of the currently executing script?
We can find the filename of the currently executing script by using the PHP superglobal variable $_SERVER. The complete method would be to use $_SERVER['PHP_SELF'].
Conclusion
In this article, We learnt about what a superglobal variable is. We also learned about the various predefined PHP superglobals available in PHP. We also saw implementation examples about these predefined PHP superglobals.
For more information about PHP, check out articles by Coding Ninjas on PHP.