Superglobal variables in PHP

php Jun 26, 2019

A couple of predefined variables in PHP are "superglobals", which suggests that they are continually accessible, paying little regard to scope - and you can get to them from any limit, class or record without taking any kind of action outstanding.

The PHP superglobal variables are:

  • $GLOBALS
  • $_SERVER
  • $_REQUEST
  • $_POST
  • $_GET
  • $_FILES
  • $_ENV
  • $_COOKIE
  • $_SESSION

So let's see them one by one,

1) $GLOBALS

$GLOBALS is a PHP excessively global variable which is utilized to get to global variable from anyplace in the PHP content (additionally from inside capacities or techniques).

PHP stores every single global variable in a cluster called $GLOBALS[index]. The file holds the name of the variable.

The model beneath tells the best way to utilize the too global variable $GLOBALS:

<?php 
$x = 5; 
$y = 5;
 
function multiply() { 
    $GLOBALS['z'] = $GLOBALS['x'] * $GLOBALS['y']; 
}
 
multiply(); 
echo $z; 
?>

In the model above, since z is a variable present inside the $GLOBALS cluster, it is likewise available from outside the function!

2) $_SERVER

$_SERVER is a PHP  global variable cluster which holds data/information about headers, paths, and scripts location.

The model beneath tells the best way to utilize a portion of the components in $_SERVER:

Code Description
$_SERVER['PHP_SELF'] Returns the filename of the currently executing script
$_SERVER['GATEWAY_INTERFACE'] Returns the version of the Common Gateway Interface (CGI) the server is using
$_SERVER['SERVER_ADDR'] Returns the IP address of the host server
$_SERVER['SERVER_NAME'] Returns the name of the host server (such as blog.smartcodehub.com)
$_SERVER['SERVER_SOFTWARE'] Returns the server identification string (such as Ghost/2.2.24)
$_SERVER['SERVER_PROTOCOL'] Returns the name and revision of the information protocol (such as HTTP/1.1)
$_SERVER['REQUEST_METHOD'] Returns the request method used to access the page (such as POST)
$_SERVER['REQUEST_TIME'] Returns the timestamp of the start of the request (such as 1377687496)
$_SERVER['QUERY_STRING'] Returns the query string if the page is accessed via a 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 complete URL of the page from which the current page was called
$_SERVER['HTTPS'] Is the script queried through a secure HTTP protocol
$_SERVER['REMOTE_ADDR'] Returns the IP address from where the user is viewing the current page
$_SERVER['REMOTE_HOST'] Returns the Host name from where the user is viewing the current page
$_SERVER['REMOTE_PORT'] Returns the port being used on the user's machine to communicate with the web server
$_SERVER['SCRIPT_FILENAME'] Returns the absolute pathname 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 runs on a virtual host, it will be the value defined for that virtual host) (such as someone@smartcodehub.com)
$_SERVER['SERVER_PORT'] Returns the port on the server machine being used by the web server for communication (such as 80)
$_SERVER['SERVER_SIGNATURE'] Returns the server version and virtual host name which are added to server-generated pages
$_SERVER['PATH_TRANSLATED'] Returns the file system based path to the current script
$_SERVER['SCRIPT_NAME'] Returns the path of the current script
$_SERVER['SCRIPT_URI'] Returns the URI of the current page

3) $_REQUEST

PHP $_REQUEST is used to collect data after submitting an HTML form with the help form method it can be either GET or POST.

The model beneath demonstrates a structure with an info field and a submit catch. At the point when a client submits the information by tapping on "Submit", the structure information is sent to the record indicated in the activity characteristic of the <form> tag. In this model, we point to this record itself for preparing structure information. On the off chance that you wish to utilize another PHP record to process structure information, supplant that with the filename of your decision. At that point, we can utilize the overly worldwide variable $_REQUEST to gather the estimation of the information field:

<html>
<body>

<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
  Email: <input type="email" name="email">
  <input type="submit">
</form>

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // collect value of input field
    $email = $_REQUEST['email'];
    if (empty($email)) {
        echo "Email is empty";
    } else {
        echo $email;
    }
}
?>

</body>
</html>

4) $_POST

PHP $_POST is broadly used to gather structure information subsequent to presenting a HTML structure with method="post". $_POST is additionally generally used to pass variables.

<html>
<body>

<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
  Website: <input type="text" name="website">
  <input type="submit">
</form>

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // collect value of input field
    $website = $_POST['website'];
    if (empty($website)) {
        echo "Website is empty";
    } else {
        echo $website;
    }
}
?>

</body>
</html>

5) $_GET

PHP $_GET can also be used to collect form data after submitting an HTML form with method="get".

$_GET can also collect data sent in the URL.

https://www.example.com\email=youremail@mail.com

In the above code you can see the form field value is passed in url when we use GET Method in php form.

You can utilize these overly Global variables in your PHP content according to your need.

Abhishek

Hi There! I Am Abhishek Developer, Singer & Photographer. Living In Mumbai. My interests range from technology to entrepreneurship. I am also interested in food, designing, and education.