In the previous chapter, we had discussed how you can access global variables using the $GLOBALS superglobal array.

By playing this video, you agree to YouTube's Terms
Watch on YouTube →

Apart from that, PHP also offers a couple of other superglobal variables, which can be accessed from anywhere in your code. The following are the ones given in the PHP documentation:

  2. $_SERVER
  3. $_REQUEST
  4. $_POST
  5. $_GET
  6. $_FILES
  7. $_SESSION
  8. $_COOKIE
  9. $_ENV

Let’s have an overview on each of them, starting with the $_SERVER variable:



Some of the array keys in this variable are:

  • PHP_SELF – gives the path of the current script
  • SERVER_NAME – gives the name of the server, in this case codelab.local
  • REQUEST_SCHEME – whether it’s http or https request


This array contains the variables passed to the PHP script in the form of query string. Can be used to access HTTP GET request parameters. Consider the following script with an HTML form:


if(isset($_GET['fname'])) {
    echo "The name you entered is: " . $_GET['fname'];

<form action="<?php echo $_SERVER['PHP_SELF']?>" method="GET">
    <input type="text" name="fname" value="Abhinav">
    <input type="submit" value="submit">

You can see how I used both $_GET and $_SERVER arrays in the above code.


Similar to $_GET$_POST contains the HTTP POST variables.


if(isset($_POST['fname'])) {
    echo "The name you entered is: " . $_POST['fname'];

<form action="<?php echo $_SERVER['PHP_SELF']?>" method="POST">
    <input type="text" name="fname" value="Abhinav">
    <input type="submit" value="submit">

This array contains the cookies sent to the server by the browser.

Try the following in test.php, which sets the cookie in the visitor’s browser:

if (setcookie("site_title", "Coding Reflections") === true) {
    echo 'title set successfully';

Now, create another file – test-2.php, and open it in browser. You should see the site title value accessed from the cookie.

if(isset($_COOKIE['site_title'])) {
    echo 'The site title is: ' . $_COOKIE['site_title'];
else {
    echo 'Site title not set';

Cookies are persistent, so the value remains there even if the browser is closed.


This variable is similar to cookies, albeit the values get destroyed when the browser is closed. For eg: can be used to save user logins during a session.

To create a new session variable:


$_SESSION['user_id'] = 25;

Now try this another PHP file and open in browser:


if(isset($_SESSION['user_id'])) {
    echo 'you are logged in as: ' . $_SESSION['user_id'];
else {
    echo 'please login';

To unset a session variable:




The $_REQUEST variable contains the contents of $_POST$_GET, and $_COOKIE superglobals.


Contains the files uploaded using an HTML form with multipart/form-data enctype.


    if(!empty($_FILES['myfile'])) {
?><form action="<?= $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
<input type="file" name="myfile" id="myfile">
<input type="submit" value="Upload">


Environment variables are now a standard and secure way to store sensitive information, such as API keys, database passwords, etc.

The $_ENV superglobal variable contains the environment variables exposed to the PHP parser by the OS or the web server.


However, it should be enabled in the php.ini file, otherwise the variable will be empty.

the getenv() function is an alternative way to access environment variables, even when the $_ENV is disabled