PHP Global Variables – Superglobals

Last updated on March 20, 2023 by Ayaz Ali Shah

In PHP, superglobal variables are accessible anywhere in the function, class, and PHP file or script. For example, if you want to access it inside the function then you can directly access it without passing it as an argument.

Following are the PHP’s superglobal variables,

  • $_POST
  • $_GET
  • $_SERVER
  • $_FILES
  • $_ENV
  • $_COOKIE

PHP Superglobal – $GLOBALS

The Superglobal $GLOBALS stores values in an array, in which the index refers to the name of the variable.

$GLOBALS['studentName'] = 'John';
echo $studentName;
// Output: John


To use the superglobal variable $GLOBALS look at the below example in which there are two variables with integer values. These variables are used inside the sum() function through the $GLOBALS variable, and the result of both variables is stored in the new global variable that is the result.

$valueOne = 10;
$valueTwo = 20;

function sum(){
	$GLOBALS['result'] = $GLOBALS['valueOne'] + $GLOBALS['valueTwo'];

echo $result;
Output: 30

PHP Superglobal – $_POST

The $_POST collects HTML form data (with method=”post”) from incoming requests and it is accessible in all scopes which means you can access it inside a function, class, or any PHP script.


To the superglobal $_POST variable, we need an HTML form that should have a post method.

<form action="your_post_file.php" method="post">
  Student Name: <input type="text" name="student_name">
  <input type="submit">

Next, we can collect the above form data through the $_POST variable.

echo $_POST['student_name'];
// Output: student_name input field value

So when you submit the above form, it will send data to the server in the request body and store it in the $_POST superglobal variable.

PHP Superglobal – $_GET

The $_GET collects data from both HTML forms and URL parameters. You can access it in any function, class or PHP file.


To get values from HTML form or query string, you need to add parameter’s key into $_GET variable as index.

// Output: it will return parameter value

If you want to get values from URL parameter then you just need to use in the same way as we used above. For example, to get the value of student_name parameter from below link

You can use $_GET superglobal variable along student_name key, below is the example,

// get form data from URL parameter
echo $_GET['student_name'];
// Output: john

PHP Superglobal – $_REQUEST

The $_REQUEST superglobal variable collects data from incoming requests in PHP. It works with both methods (GET and POST).


To use the superglobal variable $_REQUEST, let’s use the HTML form that we used in the previous example.

<form action="request.php" method="post">
  Student Name: <input type="text" name="student_name">
  <input type="submit">

In the above HTML form, we added request.php in the action attribute which means this form will be sending data to request.php. So create a PHP file with the name request.php and paste the following script to get the HTML form data,

// get form data from the incoming request
echo $_REQUEST['student_name'];

Share on:

Related Posts

Tags: PHP,