Global Variables - Superglobals

Total Post:113

Points:791
PHP
 330  View(s)
Ratings:
Rate this:

why Global Variables are Superglobals in php explain with a example ?

  1. Post:140

    Points:984
    Re: Global Variables - Superglobals

    PHP Global Variables - Superglobals :

    it's a predefined variables in PHP which are always accessible of scope and you can access them from any function and class.  The PHP superglobal variables are: 

    $GLOBALS 
    $_SERVER
    $_REQUEST
    $_POST
    $_GET
    $_FILES
    $_ENV
    $_COOKIE
    $_SESSION


    PHP $GLOBALS:

    $GLOBALS[index] in PHP it means which is stores all global variables in array. The index holds the name of the variable. 

    <?php 
    $x =10; 
    $y =20;
    function addition() { 
        $GLOBALS['z'] = $GLOBALS['x'] + $GLOBALS['y']; 
    }
    addition(); 
    echo $z; 
    ?>
    


    PHP $_SERVER :

    it is holds information about headers, paths and script locations in PHP super global variable.

    <?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'];
    ?>

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!