What is $_REQUEST in PHP?

$_REQUEST

$_REQUEST is a superglobal variable in PHP that contains input from $_POST, $_GET, and $_COOKIE. These correspond to HTTP POST, GET, and Cookies.

Usage

The following example shows how \$_REQUEST can be used:

<?php
// Choosing input type
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Getting data from input
$shotname = htmlspecialchars($_REQUEST['shot']);
echo $shotname
}
?>

This example shows how $_REQUEST can be used once a request has been made:

  • The if statement checks whether it matches the required method. Here, it checks if it is a POST request.
  • Then, it gets the data using $_REQUEST['shot']. As can be seen, $_REQUEST acts as an associative array as it uses a key to access data.
  • Once data is collected, it is converted to HTML, using htmlspecialchars(), and displayed.

Free Resources

Copyright ©2024 Educative, Inc. All rights reserved