PHP Global Variables

In this tutorial, we will learn about PHP Global Variables. The global variables are also known as superglobals and These these superglobal variables first time introduced in PHP 4.1.0 on 10 December 2001. A very interesting thing about these superglobal variables is these variables are always accessible. You can access them in different PHP files easily. Please check out the following list of superglobals:

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

$GLOBALS:

The $GLOBALS global variable is used to access global variables from anywhere(functions, methods) in the script. In PHP all the global variables in an array which is known as $GLOBALS[index]. In the index, we write the name of the variable to fetch or to add value in the variable. To understand it wisely, please checks out the following example program.

<?php
$num1 = 40;
$num2 = 30;
 
function sum() {
  $GLOBALS['ans'] = $GLOBALS['num1'] + $GLOBALS['num2'];
}

sum();
echo $ans;
?>

$_SERVER

The $_SERVER global variable holds the information about the about headers, paths, and script locations.

List of important $_SERVER elements:

Element Description
$_SERVER[‘PHP_SELF’] This returns the name of the currently executing script file.
$_SERVER[‘GATEWAY_INTERFACE’] This returns the version of the Common Gateway Interface (CGI) the server is using.
$_SERVER[‘SERVER_ADDR’] This returns the IP address of the host server.
$_SERVER[‘SERVER_NAME’] This returns the name of the host server (For example www.owlbudy.com).
$_SERVER[‘SERVER_SOFTWARE’] This returns the server identification string (For example Apache/2.2.24).
$_SERVER[‘SERVER_PROTOCOL’] This returns the name and revision of the information protocol (For example HTTP/1.1).
$_SERVER[‘REQUEST_METHOD’] This returns the request method which is used to access the page (For example POST).
$_SERVER[‘REQUEST_TIME’] This returns the timestamp of the start of the request (For example 1245124512).
$_SERVER[‘QUERY_STRING’] This returns the query string in case the page is accessed using a query string.
$_SERVER[‘HTTP_ACCEPT’] This returns the accept header from the current request.
$_SERVER[‘HTTP_ACCEPT_CHARSET’] This returns the Accept_Charset header from the current request.
$_SERVER[‘HTTP_HOST’] This returns the header of host from current request.
$_SERVER[‘HTTP_REFERER’] This returns the complete URL of current page.
$_SERVER[‘HTTPS’] This is the script queried via a secure HTTPS protocol.
$_SERVER[‘REMOTE_ADDR’] This returns the IP address of users machine from which user is communicating.
$_SERVER[‘REMOTE_HOST’] This returns name of host on user’s machine.
$_SERVER[‘REMOTE_PORT’] This returns port number of users machine which is using for web communication.
$_SERVER[‘SCRIPT_FILENAME’] This returns absolute path name of currently executing script.
$_SERVER[‘SERVER_ADMIN’] This returns the information about the server admin.
$_SERVER[‘SERVER_PORT’] This returns the port number which a machine using for web communication.
$_SERVER[‘SERVER_SIGNATURE’] This return version of server and name of the host.
$_SERVER[‘PATH_TRANSLATED’] This return path to script according of file path system.
$_SERVER[‘SCRIPT_NAME’] This returns the path of current script.
$_SERVER[‘SCRIPT_URI’] This returns the URI of current page.

Example Program:

<?php
echo $_SERVER['PHP_SELF'];
echo "<br>";
echo $_SERVER['HTTP_REFERER'];
echo "<br>";
echo $_SERVER['HTTP_HOST'];
echo "<br>";
echo $_SERVER['SERVER_NAME'];
echo "<br>";
echo $_SERVER['SCRIPT_NAME'];
?>

$_REQUEST

The $_REQUEST global variable is used to collect data from form.

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
  // collect value of input field
  $name = $_REQUEST['name'];
  if (empty($name)) {
    echo "It's empty.";
  } else {
    echo $name;
  }
}
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
  Name: <input type="text" name="name">
  <input type="submit">
</form>

$_POST

The $_GET global variable is used to collect data from the form in case data is submitted using the POST method. To understand it wisely please check out the following example program.

<?php
if(isset($_POST["Submit"])){
    echo "<p-->Hello, " . $_POST["name"] . "<p></p>";
}
?>
<form method="POST">
    <input type="text" name="name">
    <input="" value="Submit">
</form>

$_GET

The $_GET global variable is used to collect data from the form in case data is submitted using the GET method. To understand it wisely please check out the following example program.

<?php
if(isset($_GET["Submit"])){
    echo "<p-->Hello, " . $_GET["name"] . "<p></p>";
}
?>
<form method="GET">
    <input type="text" name="name">
    <input="" value="Submit">
</form>

$_FILES

The $_FILES is a two-dimensional associative global array of items which are being uploaded by via HTTP POST method.

$_ENV

The $_ENV is used to return the environment variables from the web server.

$_COOKIE

The $_COOKIE global variable is used to store some data on client’s machines in the form of a text file. The main motive of storing cookies on the client machine is to identify the user when user will revisit a site. 

<?php
$cookie_name = "user_name";
$cookie_value = "Rahul";
setcookie($cookie_name, $cookie_value); 
?>

Click here to learn about $_COOKIE global variable in detail.

$_SESSION

The $_SESSION is global variable which is used to store information and to be used across multiple pages. To understand it wisely please check out the following example program.

<?php
// Starting the session
session_start();
// Setting session variables and values
$_SESSION["login_status"] = "true";
$_SESSION["username"] = "Abc";
?>

Click here to learn about $_SESSION global variable in detail.

Spread the love
Scroll to Top
×