Getting Started with PHP – Part 6 (Superglobals)

Guest Posts Paolo Nikko Nuñal

PHP Global Variables are also called superglobals and they are accessible everywhere on your code regardless of the scope. You can access them in any part of the file without adding additional code or whatsoever.

These are the Super Global Variables.


  • $GLOBALS

  • $SERVER

  • $REQUEST

  • $POST

  • $GET

  • $FILES

  • $ENV

  • $COOKIE

  • $SESSION


Super Global variables are ordinary array variables. What makes them extra special is the fact they can be access anywhere in your PHP file. Each super global variable has a unique function and they can do wonders for your code in doing page request or simply passing data from one page to another.

We have talk about it as arrays, in fact they are associative arrays. You access them by keys to get the value of a specific index you want.

The first one on our list the $_POST. This is the most common global variable in collecting data from an HTML form with the method ="POST".

<html>
<body>
   <form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
      Name: <input type="text" name="first_name">
      Age:  <input type="text" name="age">
    <input type="submit" value="Submit">
</body>
</html>


Once you select the submit button, automatically, the values of the inputted data will be assigned to the $POST array. From our example we have two values namely the Name, and Age with the name "firstname" and "age" respectively. The names of the input will be placed in an associative array with the $POST variable. Shown like these if you do a vardump($POST).

$
POST['firstname'] = "";
$
POST['age'] = ""

The same as true with the $GET global variable. All you have to do is change the method from post to get.

So you might ask what is the difference between Get and Post?

When you use GET, the variables inputted will be visible in the URL. Like say www.caveofprogramming.com/articles?first
name="John"&age=23

Unlike with POST it is invisible to others, thus making it secure and safe.