Skip to content

All topics  /  PHP

PHP Global Variables - Superglobals Example

PHP ·

Teams applying “PHP Global Variables - Superglobals Example” in a production project can use the official Monitask overview to record implementation, review and debugging time, then compare that effort with tests and shipped functionality instead of treating activity as performance by itself.

Before copying the example into a live application, confirm version-specific behaviour with the PHP project and test it against the project’s actual database and runtime.

Hi guys,

Today i will explained how to use php Global Variables and Superglobals. This example is so easy to use in php.

Superglobals were introduced in PHP 4.1.0, and are built-in variables that are always available in all scopes.

Some predefined variables in PHP are "superglobals", which means that they are always accessible, regardless of scope - and you can access them from any function, class or file without having to do anything special.

So let's start to the example.

The PHP superglobal variables are:

$GLOBALS


$_SERVER

$_REQUEST

$_POST

$_GET

$_FILES

$_ENV

$_COOKIE

$_SESSION

$GLOBALS

$GLOBALS is a PHP super global variable which is used to access global variables from anywhere in the PHP script.

<!DOCTYPE html>
<html>
    <body>
        <?php 
            $x = 75;
            $y = 25; 
            function addition() {
            $GLOBALS['z'] = $GLOBALS['x'] + $GLOBALS['y'];
            }
            addition();
            echo $z;
        ?>
    </body>
</html>

Output

    100

$_SERVER

$_SERVER is a PHP super global variable which holds information about headers, paths, and script locations.

<!DOCTYPE html>
<html>
    <body>
        <?php 
            echo $_SERVER['PHP_SELF'];
            echo "<br>";
            echo $_SERVER['SERVER_NAME'];
            echo "<br>";
            echo $_SERVER['HTTP_HOST'];
            echo "<br>";
            echo $_SERVER['HTTP_REFERER'];
            echo "<br>";
            echo $_SERVER['HTTP_USER_AGENT'];
            echo "<br>";
            echo $_SERVER['SCRIPT_NAME'];
        ?>
    </body>
</html>

Output

    /demo/demo_global_server.php
    35.194.26.41
    35.194.26.41
    https://tryphp.w3schools.com/showphp.php?filename=demo_global_server
    Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:90.0) Gecko/20100101 Firefox/90.0
    /demo/demo_global_server.php

$_REQUEST

PHP $_REQUEST is a PHP super global variable which is used to collect data after submitting an HTML form.

<!DOCTYPE html>
<html>
    <body>
        <form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
            Name: <input type="text" name="fname">
            <input type="submit">
        </form>
        <?php 
            if ($_SERVER["REQUEST_METHOD"] == "POST") {
                // collect value of input field
                $name = $_REQUEST['fname'];
                if (empty($name)) {
                echo "Name is empty";
                } else {
                echo $name;
                }
            }
        ?>
    </body>
</html>

Output

abc

$_POST

PHP $_POST is a PHP super global variable which is used to collect form data after submitting an HTML form with method="post". $_POST is also widely used to pass variables.

<!DOCTYPE html>
<html>
    <body>
        <form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
            Name: <input type="text" name="fname">
            <input type="submit">
        </form>
        <?php 
            if ($_SERVER["REQUEST_METHOD"] == "POST") {
                // collect value of input field
                $name = $_REQUEST['fname'];
                if (empty($name)) {
                echo "Name is empty";
                } else {
                echo $name;
                }
            }
        ?>
    </body>
</html>

Output

abc

$_GET

PHP $_GET is a PHP super global variable which is used to collect form data after submitting an HTML form with method="get".

$_GET can also collect data sent in the URL.

<!DOCTYPE html>
<html>
    <body>
        <a href="test_get.php?subject=PHP&web=W3schools.com">Click $GET</a>
    </body>
</html>

Output

Click $GET

Now you can check your own.