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

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 200
    Comment on it

    PHP contains many predefined variables from these predefined variables many are superglobals, It means that we can access all these variables from any function,file or class without having any problem.

    Below are some PHP superglobal variables:-

    • $GLOBALS
    • $_SERVER
    • $_REQUEST
    • $_POST
    • $_GET

    PHP $GLOBALS

    $GLOBALS  is a part of superglobal variables . It is use for accessing global variables.

    eg

    <?php
    $a = 5;
    $b = 10;
     
    function add() {
        $GLOBALS['c'] = $GLOBALS['a'] + $GLOBALS['b'];
    }
     
    add();
    echo $c;
    ?> 

     

    In the above example  c is a global variable which is contain  within the $GLOBALS array, so we can access it from outside the function.

     

    PHP $_SERVER

    $_SERVER contains information about headers, paths, and script.

    eg:-

    <?php
    echo $_SERVER['SERVER_NAME'];
    echo "<br>";
    echo $_SERVER['HTTP_HOST'];
    echo "<br>";
    echo $_SERVER['HTTP_USER_AGENT'];
    echo "<br>";
    echo $_SERVER['SCRIPT_NAME'];
    ?> 

     

    PHP $_REQUEST

    PHP $_REQUEST variable will help us in collecting information when we submit an HTML form.

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

    PHP $_POST

    when we submit html form with post method than $_post is used to pass variables.

    eg:-

    <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 = $_POST['fname'];
        if (empty($name)) {
            echo "Name is empty";
        } else {
            echo $name;
        }
    }
    ?>
    </body>
    </html>

    PHP $_GET

    when we submit html form with GET method than $_GET is used to pass variables.

    <html>
    <body>
    
    <?php
    echo "name "  $_GET['fname']];
    ?>
    
    </body>
    </html>

     

 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: