i

PHP Tutorial

Superglobals

Some variables in php are always accessible, and they have a global scope and are predefined. Those variables are known as SuperGlobals. You can access these super globals from any file, class or a function. We have below super globals in php.

  • $GLOBALS

  • $_SERVER

  • $_REQUEST

  • $_POST

  • $_GET

  • $_FILES

  • $_ENV

  • $_COOKIE

  • $_SESSION

Let us discuss few super globals in this section and other we will discuss in the later sections.

>$GLOBALS

$GLOBALS is a superglobal variable that allows us to access global variables whenever and wherever required in the php script. You can even call these global variables from methods or functions. $GLOBALS[] array in php stores all the global variables and the variable name is present under the index of this array.

For example, in the below code, the global variable can be accessed outside the function also.

$a = 25; $b = 5;

function division()

{

           $GLOBALS[‘x’] = $GLOBALS[‘a’] / $GLOBALS [‘b’];

}

division();

echo $x;

?>

The output of the above-written code will be 5.

>$_SERVER

$_SERVER is a super global that helps us to store the header, paths and script location information. You can use this super global variable as below to retrieve the information.

echo “ $_SERVER[‘SERVER_NAME’];

echo “
”;

echo “$_SERVER[‘PHP_SELF’];

echo “
”;

echo “$_SERVER[‘HTTP_HOST’];

echo “
”;

echo “$SERVER[‘SCRIPT_NAME’];

echo “
”;

echo “$_SERVER[‘HTTP_USER_AGENT’];

echo “
;

?>

The output will show server name, the file you are currently working on, HTTP hostname, script name and user agent.

>$_REQUEST

$_REQUEST is a super variable that collects the information when you submit an HTML form after you submit the data in a form. Let us see in the below example.

 Name:

 if ($_SERVER["REQUEST_METHOD"] == "POST") {

    // collect the name of file

   $name = $_REQUEST['file_name'];

    if (empty($name)) {

        echo "Name is not entered";

   } else {

        echo $name;

   }

 }

 ?>

When the user enters input and submit the button, data is sent to the file present in the action attribute of the form tag and $_REQUEST super global is used to collect that information from the form.

>$_POST

$_POST is a super global variable that collects the information users submit on a form similar like request method, but while using the Post method for transferring data, the data is not visible in the query string. We can use $_POST super global variable as below.

 Name:

 if ($_SERVER["REQUEST_METHOD"] == "POST") {

    // collect value of input field

   $name = $_POST['file_name'];

    if (empty($name)) {

        echo "Name is not entered";

   } else {

        echo $name;

   }

 }

 ?>

When the user enters input and submit the button, data is sent to the file present in the action attribute of the form tag and $_POST super global is used to collect that information from the form without displaying the query string. 

>$GET 

$_GET is a super global variable that collects the information users submit on a form similar like Post method but while using get method for transferring data, the data is visible in the query string and values are not hidden. It stores the value that comes in the URL like filename, username, password etc. We can use $_GET super global variable as below.

if( $_GET["username"] || $_GET["age"] ) 

{

echo "Welcome ". $_GET['username']. "
";

echo "You are ". $_GET['age']. " years old.";

     exit();

  }

?>

        Name:

        Age: