CommonLounge Archive

PHP: Superglobals and Forms

September 19, 2018

One of the most powerful features of PHP is the way it handles HTML forms. The basic concept is that any form element will automatically be available to your PHP scripts! How? Let’s take a look:

Variable Scope

Remember how to write your own functions? Now, try writing a simple PHP code like this:

    function test() {
        $x = 5;
    echo $x;

Save the file and refresh Chrome. What do you see?

Notice: Undefined variable: x in...

Why did this happen? We did define and set $x = 5 in the function test(). Okay, maybe if we call the function, PHP will declare the variable for us. Try adding the function call like this:

echo $x;

And run it:

Notice: Undefined variable: x in...

What?! We are still getting the same error.

This is because of something known as scope of a variable. The variable $x has a local scope which means it can only be accessed inside the function test() and not anywhere else outside it.

Okay, so what if we did want to access a variable defined in a function outside of it anyway? And what about the converse? If we declare a variable outside a function, can we use it inside that function?

Yes! PHP allows us to do both of these using the variable called $GLOBALS. It is an associative array with the name of the variable as key and value as the array element.

We can use this array variable to add an array of PHP variables in a global scope i.e. they can also be accessible from anywhere!

Here is an example:

    $x = 3;
    $y = 5;
    function multiply() {
        $GLOBALS['z'] = $GLOBALS['x'] * $GLOBALS['y'];
    echo $z; // prints 15

In the example above, since z is a variable present within the $GLOBALS array, it is also accessible from outside the function! So, all three variables x, y and z have global scope.


Now, try running this code:


You will see a lot of elements but somewhere, you will find this:

["x"]=> int(3) ["y"]=> int(5) ["z"]=> int(15)

As we mentioned earlier, $GLOBALS is an associative array which has a global scope. Simply put, $GLOBALS is a superglobal.

Several predefined variables in PHP are superglobals, which means that they are always accessible, regardless of scope - and you can access them from anywhere without having to do anything special. This is awesome, right?

We will look at a couple more superglobals in this tutorial, and the rest would be covered later in the course.

HTML forms and requests

When you send a letter, it needs to have certain features to be delivered correctly: an address, a stamp, etc. You also use a language that the receiver understands, right? The same applies to the data packets you send to see a website. We use a protocol called HTTP (Hypertext Transfer Protocol).

So when you have a website, you need to have a server (machine) where it lives. When the server receives an incoming request (letter) from someone accessing your website (known as client), it sends back a response (another letter) to the client.

HTTP has several methods to handle such requests and responses. The two most common HTTP methods are: GET and POST.


GET is used to request data from a specified resource. This is what a HTML form which uses the GET method looks like:

<html lang="en">
    <form action="" method="get">
      Name: <input type="text" name="username" /><br />
      Age: <input type="number" name="age" /><br />
      <input type="submit" name="submit" value="submit" />

Save the file and refresh your browser. This is how the page would look like:

Go ahead, type in your Name and Age and press submit. This is how the URL will look like:


Do you see a ? followed by key-value pairs of the data you just entered?

Amazing! We just got the information filled in by the user in the URL of the page.

Note: In this case, the GET method requests the data from the HTML form (when we press submit) and the query string (key-value pairs) is sent in the URL of the GET request.

But what if we wanted to input important information like Passwords, Credit Card details etc.? We don’t want such important information to be shown in the URL right?

Thus, GET requests should never be used when dealing with sensitive data.


This is where the POST request comes into picture. POST is used to send data to a server to create/update a resource.

To implement the POST method, everything in the HTML form remains the same except this line:

<form action="" method="post">

Make this change and refresh your browser. The form looks the same. But now, try to fill in your Name and Age and press submit. Do you see any change in the URL?

You won’t. But the key value pairs of the data you just entered are still available. We just don’t see it!

Note: The data sent to the server with POST is stored in the request body of the HTTP request and not in the URL.

In both cases, the form elements (like Name, Age etc) are made available to be used by our PHP scripts. So now let’s look at how we can actually use this data by writing a new PHP script!

PHP Forms

Now, the user fills in the form and presses submit. Let’s say we then want to redirect the user to another page where we simply display the details he/she just entered. This can be done by setting the action attribute of the form element in HTML:

<form action="action.php" method="post">

This implies that we want to send a POST request and redirect the user to a new page action.php when the user clicks on the submit button. Easy, right?

Let’s create the a new file in htdocs/basics/ folder and call it action.php.

In this page, we want to display the Name and Age of the user that the user just entered. So basically, we want to access the form elements Name and Age and display it on the browser.

This is where we will use another superglobal called $_POST:

// action.php
    echo "Hello " . $_POST['username'] . "!<br>" ;
    echo "You are " . $_POST['age'] . " years old.";

Now again, go ahead and enter your Name and Age and press submit. This is what you will see:

Hello Commonlounge!
You are 22 years old.

This can be done using the GET method too. In the HTML, just change the method attribute to "get" and keep everything else as it is.

<form action="action.php" method="get">

We also need to change action.php in order to fetch data from a GET request. We will use a superglobal called $_GET in this case:

    echo "Hello " . $_GET['username'] . "!<br>" ;
    echo "You are " . $_GET['age'] . " years old.";

This will produce the same output when the user clicks on submit:

Hello Commonlounge!
You are 22 years old.

Note: While using $_GET or$POST, the name of the index must match with the name attribute specified in the form element. For example: <input type="number" name="age" /> has the name attribute set to "age". Hence, `$GET[‘age’]or$_POST[‘age’]will give us the value present in theAge` field.


You made it! This was an important chapter, as PHP Forms and Superglobals are used in every real world PHP project! You are now ready to build your own PHP project from scratch? Go ahead!

© 2016-2022. All rights reserved.