Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • PHP superglobals

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 122
    Comment on it

    PHP has a large number of variable the are defined already.

    PHP provide an additional set of predefined arrays containing variables from the web server.

    PHP superglobal

    1. $GLOBALS- It got the reference to every variable in the global scope.

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

    2. $_SERVER- It is an array that has information about the header, path, script location.

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

    3. $_GET- It is an associative array of variable passed to the current script by HTTP GET method.

    <?php
    echo "Study " . $_GET['subject'] . " at " . $_GET['web'];
    ?>

    4. $_POST- It is an associative array of variable passed to the current script by HTTP POST method.

    <?php
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        // collect value of input field
        $name = $_POST['fname'];
        if (empty($name)) {
            echo "Name is empty";
        } else {
            echo $name;
        }
    }
    ?>

    5. $_FILES- It is an associative array of uploaded item by HTTP POST method.

    6. $_REQUEST- It is an associative array of content by HTTP $_GET and $_POST method.

    <?php
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        // collect value of input field
        $name = $_REQUEST['fname'];
        if (empty($name)) {
            echo "Name is empty";
        } else {
            echo $name;
        }
    }
    ?>

    7. $_COOKIE- It is an associative array of variable by HTTP cookies.

    8. $_SESSION- It is an associative array of session variables.

    9. $_PHP_SELF- It is a string that got the name of the script file which it is called.

    10- $php_erromsg- It is the variable that got the last error message generated by php.

    ex

     

     

     

     

     

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: