PHP super globals are built-in variables that can be accessed from all scopes.
PHP Super Globals (PHP Super Globals)
In this lesson, we will explain all super globals in PHP with examples. Read on to find out.
PHP Super Globals: $GLOBALS
The $GLOBALS superglobal is used to access global variables from any field (including functions or methods) in the PHP script. PHP stores all global variables in an array called $GLOBALS[index]. The index holds the name of the variable. The following example shows how to use the $GLOBALS superglobal variable:
$number1 = 50;
$number2 = 20;
function addition(): int
{
return $GLOBALS[ 'number3' ] = $GLOBALS[ 'number2' ] + $GLOBALS[ 'number1' ];
}
echo addition();
// Çıktı: 70PHP Super Globals: $_REQUEST
PHP $_REQUEST is PHP superglobal used to collect data from HTML form.
<!DOCTYPE html>
<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') {
// input elementinden gelen veriyi değişkene ata
$name = htmlspecialchars($_REQUEST[ 'fname' ]);
if (empty($name)) {
echo 'Ad yok';
} else {
echo $name;
}
}
?>
</body>
</html>In the example above, we created a form that returns data with the post method. We enabled it to return the data on the same page with $_SERVER['PHP_SELF']. We verified that the request coming with the $_SERVER['REQUEST_METHOD'] superglobal is a post request. Then, we safely assigned the data from the input named fname with the name value to the $name variable with the $_REQUEST superglobal using the htmlspecialchars() function. We checked whether the $name variable is empty or not with the PHP if condition. If it is empty, we print the output "No name", otherwise we print the data of the $name variable.
PHP Super Globals: $_POST
PHP $_POST is a PHP superglobal used to collect data from the HTML form via the POST method.
<!DOCTYPE html>
<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') {
// input elementinden gelen veriyi değişkene ata
$name = htmlspecialchars($_POST[ 'fname' ]);
if (empty($name)) {
echo 'Ad yok';
} else {
echo $name;
}
}
?>
</body>
</html>In the example above, we created a form that returns data with the post method. We enabled it to return the data on the same page with $_SERVER['PHP_SELF']. We verified that the request coming with the $_SERVER['REQUEST_METHOD'] superglobal is a post request. Then, we safely assigned the data from the input named fname with the name value to the $name variable with the $_POST superglobal using the htmlspecialchars() function. We checked whether the $name variable is empty or not with the PHP if condition. If it is empty, we print the output "No name", otherwise we print the data of the $name variable.
PHP Super Globals: $_GET
PHP $_GET is a PHP superglobal used to collect data from the HTML form via the GET method. We can also retrieve the data sent with get in the url with the $_GET superglobal.
Getting Data from Form with PHP $_GET Super Global
In the example below, we received data from the HTML form with the PHP $_GET superglobal.
<!DOCTYPE html>
<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' ] === 'GET') {
// input elementinden gelen veriyi değişkene ata
$name = htmlspecialchars($_GET[ 'fname' ]);
if (empty($name)) {
echo 'Ad yok';
} else {
echo $name;
}
}
?>
</body>
</html>In the example above, we created a form that returns data with the post method. We enabled it to return the data on the same page with $_SERVER['PHP_SELF']. We verified that the request coming with the $_SERVER['REQUEST_METHOD'] superglobal is a post request. Then, we safely assigned the data from the input named fname with the name value to the $name variable with the $_POST superglobal using the htmlspecialchars() function. We checked whether the $name variable is empty or not with the PHP if condition. If it is empty, we print the output "No name", otherwise we print the data of the $name variable.
Getting Data from URL with PHP $_GET Super Global
In the example below, we retrieved data from the URL with the PHP $_GET superglobal.
<!DOCTYPE html>
<html>
<body>
<a href="<?php
echo $_SERVER[ 'PHP_SELF' ] ?>?name=Onur%20Özden&website=www.onurozden.com.tr">Buraya Tıkla</a>
<?php
echo "Ben " . $_GET[ 'name' ] . " şu sitede PHP öğreniyorum: " . $_GET[ 'website' ];
?>
</body>
</html>In the example above, instead of typing your file name, we got the file name using the $_SERVER superglobal. Now let's explain what we did in this example. We created an HTML document and gave the name parameter to the get.php file. We gave the name parameter the value "Onur Özden" and separated the parameters with the '&' sign. Afterwards, we created the website parameter and gave it the value "onurozden.com.tr". We printed the text "Click Here" on label A. Coming to the PHP part, we printed "My name" with echo and used the $_GET superglobal with the concatenation operator to retrieve and print the data of the parameter named name. Again, using the merge operator, we printed " and my website ". We used the concatenation operator again and got the data of the website parameter with the $GET superglobal and printed it.
PHP Super Globals: $_SERVER
The $_SERVER superglobal is a PHP superglobal variable that holds information about headers, paths, and script locations. In the example below, we show how to use the $_SERVER superglobal.
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'];In the example above, we defined variables named $number1 and $number2 and gave them values of type number. Then, we defined a function called addition that returns a value of int data type. We said that in the $GLOBALS superglobal, the names of the variables are kept as indexes. We created the variable by creating an index named number3. We assigned this variable the sum of the $number2 variable and the $number1 variable, which we accessed from the global area with the $GLOBALS superglobal. We then returned this value with return and terminated the function. As the last step, we output the value in our addition() function with echo.
All Uses of PHP $_SERVER Super Global
| $_SERVER['PHP_SELF'] | Returns the filename of the executed script. |
| $_SERVER['GATEWAY_INTERFACE'] | Returns the CGI (Common Gateway Interface) version used by the server. |
| $_SERVER['SERVER_ADDR'] | Returns the IP address of the host server. |
| $_SERVER['SERVER_NAME'] | Returns the name of the main server. |
| $_SERVER['SERVER_SOFTWARE'] | Returns the server identification text (such as Apache/2.2.24). |
| $_SERVER['SERVER_PROTOCOL'] | Returns the name and version of the information protocol (such as HTTP/1.1). |
| $_SERVER['REQUEST_METHOD'] | Returns the request method used to access the page (such as POST, GET). |
| $_SERVER['REQUEST_TIME'] | Returns the timestamp of the beginning of the request (e.g. 1377687496). |
| $_SERVER['QUERY_STRING'] | If the page is accessed via a query string, it returns the query string. |
| $_SERVER['HTTP_ACCEPT'] | Returns the accept header from the current request. |
| $_SERVER['HTTP_ACCEPT_CHARSET'] | Returns the Accept_Charset header from the current request (such as utf-8,ISO-8859-1). |
| $_SERVER['HTTP_HOST'] | Returns the host header from the current request. |
| $_SERVER['HTTP_REFERER'] | Returns the full URL of the current page (not reliable as not all user agents support it). |
| $_SERVER['HTTPS'] | Checks whether the script is queried via a secure HTTP protocol (HTTPS). |
| $_SERVER['REMOTE_ADDR'] | Returns the IP address from which the user is viewing the current page. |
| $_SERVER['REMOTE_HOST'] | Returns the hostname from where the user is viewing the current page. |
| $_SERVER['REMOTE_PORT'] | Returns the port used on the user's machine to communicate with the web server. |
| $_SERVER['SCRIPT_FILENAME'] | Returns the absolute path of the currently executing script. |
| $_SERVER['SERVER_ADMIN'] | Returns the value given to the SERVER_ADMIN directive in the web server configuration file (if your script is running on a virtual host, it will be the value defined for that virtual host) (such as some@onurozden.com.tr). |
| $_SERVER['SERVER_PORT'] | Returns the port on the server machine used by the web server for communication (such as 80). |
| $_SERVER['SERVER_SIGNATURE'] | Returns the server version and virtual hostname added to the server-generated page. |
| $_SERVER['PATH_TRANSLATED'] | Returns the file system based path to the current script. |
| $_SERVER['SCRIPT_FILENAME'] | Returns the path to the current script. |
| $_SERVER['SCRIPT_URI'] | Returns the URI of the current page. |



