Learn PHP Programming

A backend server scripting language, and a powerful tool for making dynamic and interactive Web pages. Learn each topic with examples and output.

PHP Introduction
Control Structures
Php Forms
Php Advanced

PHP Global Variables - Superglobals

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.

The PHP superglobal variables are:

PHP $GLOBALS

$GLOBALS is a PHP super global variable which is used to access global variables from anywhere in the PHP script (also from within functions or methods).

PHP stores all global variables in an array called $GLOBALS[index]. The index holds the name of the variable.

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

PHP $_SERVER

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

<?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'];
?>
/demo/demo_global_server.php 35.194.26.41 35.194.26.41 https://analyzecode.com/showphp.php?filename=demo_global_server Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36 /demo/demo_global_server.php

    <html>
        <body>
        
        <form method="post" action="">
          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>

Try to type something in input


PHP Superglobal - $_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.

The example below shows a form with an input field and a submit button. When a user submits the data by clicking on "Submit", the form data is sent to the file specified in the action attribute of the <form> tag. In this example, we point to the file itself for processing form data. If you wish to use another PHP file to process form data, replace that with the filename of your choice. Then, we can use the super global variable $_POST to collect the value of the input field:

        <html>
            <body>
            
            <form method="post" action="">
              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

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.

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

        </body>
    </html>
Study PHP at AnalyzeCode.com

Now that you have done SuperGlobals in Php, let's learn Regular Expressions in php


Share this page on :

=