0
0
PhpConceptBeginner · 3 min read

What is $_REQUEST in PHP: Explanation and Usage

$_REQUEST in PHP is a superglobal array that contains data from $_GET, $_POST, and $_COOKIE. It collects user input sent via HTTP requests, allowing you to access form data or URL parameters in one place.
⚙️

How It Works

Imagine you have a mailbox that collects letters from three different sources: your friends (GET), your family (POST), and your neighbors (COOKIE). Instead of checking each mailbox separately, $_REQUEST acts like a single mailbox that gathers all these letters together.

In PHP, $_REQUEST combines data sent through URL parameters ($_GET), form submissions ($_POST), and cookies ($_COOKIE). This means you can access any of these inputs using one array without worrying about where the data came from.

However, the order of data in $_REQUEST depends on the PHP configuration, so if the same key exists in multiple sources, the value you get might vary.

💻

Example

This example shows how to use $_REQUEST to get a user's name sent via a form or URL.

php
<?php
// Suppose the URL is: example.php?name=Alice
// Or a form sends 'name' via POST

if (isset($_REQUEST['name'])) {
    echo "Hello, " . htmlspecialchars($_REQUEST['name']) . "!";
} else {
    echo "Name not provided.";
}
?>
Output
Hello, Alice!
🎯

When to Use

Use $_REQUEST when you want a quick way to access input data without caring about the method used to send it. It is helpful for simple scripts where you accept data from forms or URLs and want to handle them uniformly.

However, for security and clarity, it is better to use $_GET, $_POST, or $_COOKIE directly when you know the exact source of data. This helps avoid confusion and potential security risks.

Real-world use cases include small contact forms, search queries, or scripts that accept parameters from multiple sources.

Key Points

  • $_REQUEST combines $_GET, $_POST, and $_COOKIE data.
  • It provides a simple way to access user input regardless of method.
  • Order of data in $_REQUEST depends on PHP settings.
  • Using specific superglobals ($_GET, $_POST) is safer and clearer.
  • Good for quick scripts but avoid in complex or secure applications.

Key Takeaways

$_REQUEST holds data from $_GET, $_POST, and $_COOKIE in one array.
It is useful for simple access to user input without checking the request method.
For security and clarity, prefer using $_GET or $_POST directly when possible.
The order of data in $_REQUEST depends on PHP configuration and can affect which value you get.
Avoid $_REQUEST in complex applications where input source matters.