Superglobal variables are built-in global variables that are always available in all scopes. Which means they are available in all scopes throughout a script. You no need to use global keyword to access them within functions or methods.
It helps to access global variables from anywhere in the PHP script. In PHP, all global variables are stored in an array called $GLOBALS[]. Index of this variable holds the name of global variable that can be accessed.
Sample Example:
<?php
$abc = 100;
$xyz = 200;
function calculateSum() {
$temp = $GLOBALS['abc'] + $GLOBALS['xyz'];
$GLOBALS['sum'] = $temp;
}
calculateSum();
echo $sum;
Output:
300
This super global variable containes information like headers, paths, and script locations. The entries in this array are created by the web server. Below are the list of mostly used informations from this variable:
Sample Example:
<?php
$temp = array(
'filename' =--> $_SERVER['PHP_SELF'],
'server_name' => $_SERVER['SERVER_NAME'],
'server_address' => $_SERVER['SERVER_ADDR'],
'host_name' => $_SERVER['HTTP_HOST'],
'request_method' => $_SERVER['REQUEST_METHOD'],
'request_uri' => $_SERVER['REQUEST_URI'],
'query_string' => $_SERVER['QUERY_STRING'],
'script_name' => $_SERVER['SCRIPT_NAME'],
'script_uri' => $_SERVER['SCRIPT_URI']
);
print_r($temp);
Output:
Array
(
[filename] => /index.php
[server_name] => www.wtechpoint.com
[server_address] => 127.0.0.1
[host_name] => www.wtechpoint.com
[request_method] => GET
[request_uri] => /?q=php
[query_string] => q=php
[script_name] => /index.php
)
This super global variable is used to collect data after submitting an HTML form. When a user submits the data by clicking on the submit button, the form data is sent to the file specified in the action attribute. $_REQUEST is used rarely, because $_POST and $_GET perform the same task and are widely used.
Sample Example:
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$fullname = $_REQUEST['fullname'];
if(empty($fullname)){
echo "Your Name is empty!!";
} else {
echo "Hi," . $fullname;
}
}
This super global variable is used to collect data from the HTML form. When you set form method as get and submit it, the form data is visible in the url. $_GET is an associative array of variables passed to the current script via the URL parameters (query string). This array is not only populated for GET requests, but rather for all requests with a query string.
Sample Example:
<?php
if ($_SERVER["REQUEST_METHOD"] == "GET") {
$fullname = $_GET['fullname'];
if(empty($fullname)){
echo "Your Name is empty!!";
} else {
echo "Hi," . $fullname;
}
}
This super global variable is used to collect data from the HTML form. $_POST is an associative array of variables passed to the current script via the HTTP POST method when using application/x-www-form-urlencoded or multipart/form-data as the HTTP Content-Type in the request. When you set form method as post and submit it, the form data is not visible in the url and provides security.
Sample Example:
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$fullname = $_POST['fullname'];
$email = $_POST['email'];
if(empty($fullname)){
echo "Your Name is empty!!";
} else {
echo "Hi," . $fullname;
}
if(empty($email)){
echo "Your email is empty!!";
} else {
echo "Your email is " . $email;
}
}
This super global variable is also called as HTTP File Upload variable and can be used to upload files. This is an associative array of items uploaded to the current script via the HTTP POST method. The structure of this array is outlined in the POST method uploads section.
Sample Example:
<?php
if($_FILES[‘file’] --> 0){
echo ‘You have selected a file to upload’;
}
This superglobal variable can be used to retrieve cookies. Cookies are small text files loaded from a server to a client computer storing some information regarding the client computer. So that when the same page from the server is visited by the user , necessary information can be collected from the cookie itself, decreasing the latency to open the page.
Sample Example:
<?php
setrawcookie();
print_r($_COOKIE);
Sessions are wonderful ways to pass variables. All you need to do is start a session by session_start() function. Then you can store and access variables in and from $_SESSION. You can access it from anywhere on the server.
Sample Example:
<?php
session_start();
$_SESSION[‘name’] = ‘WTECHPOINT’;
echo $_SESSION[‘name’];
This superglobal variable can be used to return the environment variables from the web server. It is an associative array of variables passed to the current script via the environment method.
Sample Example:
<?php
echo 'My username is ' .$_ENV["USER"] . '!';