PHP Tutorial PHP Advanced PHP References

PHP $_REQUEST Variable



The PHP $_REQUEST is a built-in super global variable which is always available in all scopes. The $_REQUEST is an associative array and by default it contains the contents of $_GET, $_POST and $_COOKIE.

The HTML form

Consider the example below where test.php contains following code. This page will send the POST variables ("name" and "age") to the action script, greetings.php.

<html>
<body>
   
  <form action="greetings.php" method="POST">
    Name: <input type = "text" name = "name" />
    Age: <input type = "text" name = "age" />
    <input type = "submit" />
  </form>
      
</body>
</html>

The test.php will look similar to:

The HTML form

The action script

Now, consider the greetings.php as the action script page which contains the following code:

<html>
<body>
   
  <?php
    echo "Hello ". $_REQUEST['name']. "<br>";
    echo "You are ". $_REQUEST['age']. " years old.";
  ?>
      
</body>
</html>

Result

If the following input is provided to the test.php:

The HTML form

When "submit" button is clicked, it will open the greetings.php with the following result:

The HTML form

Example: $_REQUEST example

The example below demonstrates the above discussed concept.

<html>
<body>
   
  <form action="greetings.php" method="POST">
    Name: <input type = "text" name = "name" />
    Age: <input type = "text" name = "age" />
    <input type = "submit" />
  </form>
      
</body>
</html>

❮ PHP - Predefined Variables

5