$_REQUEST lets you get data sent by a user through forms or URLs in one simple place.
$_REQUEST behavior in PHP
$_REQUEST['key']$_REQUEST is a PHP superglobal array that contains data from $_GET, $_POST, and $_COOKIE.
The order of data in $_REQUEST depends on the PHP configuration setting request_order or variables_order.
<?php // Access a value sent by GET, POST, or COOKIE $name = $_REQUEST['name']; echo "Hello, $name!"; ?>
<?php // Check if a value exists in $_REQUEST if (isset($_REQUEST['age'])) { echo "Age is " . $_REQUEST['age']; } else { echo "Age not provided."; } ?>
This program demonstrates that manually assigning values to $_GET, $_POST, and $_COOKIE inside the script does not populate $_REQUEST, as $_REQUEST is assembled from incoming HTTP request data before the script executes. In real scenarios, $_REQUEST prioritizes based on request_order (typically 'GP', so GET then POST).
<?php // Simulate GET and POST data $_GET['color'] = 'blue'; $_POST['color'] = 'red'; $_COOKIE['color'] = 'green'; // By default, request_order is usually 'GP' meaning GET then POST // But let's print what $_REQUEST['color'] gives echo "Color from $_REQUEST: " . $_REQUEST['color'] . "\n"; // To show the difference, print each superglobal echo "Color from $_GET: " . $_GET['color'] . "\n"; echo "Color from $_POST: " . $_POST['color'] . "\n"; echo "Color from $_COOKIE: " . $_COOKIE['color'] . "\n"; ?>
$_REQUEST can be slower and less secure because it mixes data sources.
It's better to use $_GET, $_POST, or $_COOKIE directly when you know where data should come from.
Check your PHP request_order setting to understand which data $_REQUEST uses first.
$_REQUEST combines GET, POST, and COOKIE data into one array.
It is easy to use but can cause confusion if you don't know the data source order.
Use it for quick access, but prefer specific superglobals for clarity and security.
Practice
$_REQUEST contain?Solution
Step 1: Understand the purpose of
$_REQUEST$_REQUESTis designed to collect data from multiple sources: GET, POST, and COOKIE.Step 2: Identify what
It merges these three superglobals into one array for easier access.$_REQUESTincludesFinal Answer:
Data from GET, POST, and COOKIE combined -> Option AQuick Check:
$_REQUEST= GET + POST + COOKIE [OK]
- Thinking $_REQUEST only contains GET or POST data
- Confusing $_REQUEST with $_POST or $_GET
- Assuming $_REQUEST excludes cookies
username using $_REQUEST in PHP?Solution
Step 1: Recall PHP array syntax
PHP superglobals like$_REQUESTare associative arrays accessed with square brackets and quotes for keys.Step 2: Identify correct syntax for accessing 'username'
The correct syntax is$_REQUEST['username']. Using arrows or dots is invalid for arrays.Final Answer:
$_REQUEST['username'] -> Option DQuick Check:
Array access uses brackets and quotes [OK]
- Using object notation (->) instead of array syntax
- Using dot notation like in JavaScript
- Forgetting quotes around the key
script.php?color=red and the form sends color=blue via POST?<?php print_r($_REQUEST['color']); ?>
Solution
Step 1: Understand the order of variables in $_REQUEST
By default, PHP's$_REQUESTuses the order: GET, POST, COOKIE. POST data overrides GET if keys are the same.Step 2: Determine which value
Since POST sends$_REQUEST['color']holdscolor=blue, it overrides the GETcolor=red. So,$_REQUEST['color']is 'blue'.Final Answer:
blue -> Option CQuick Check:
POST overrides GET in $_REQUEST [OK]
- Assuming GET value appears first
- Confusing $_REQUEST with $_GET or $_POST
- Expecting an array output
<?php print_r($_REQUEST['user']); ?>
If the form sends no 'user' field and the URL has no 'user' parameter, what error will occur and how to fix it?
Solution
Step 1: Identify the error from accessing missing key
Accessing$_REQUEST['user']when 'user' is not set causes a Notice: Undefined index.Step 2: Fix by checking if key exists
Useisset($_REQUEST['user'])before accessing to avoid the notice.Final Answer:
Notice: Undefined index; fix by checking with isset() -> Option BQuick Check:
Check keys with isset() to avoid notices [OK]
- Ignoring undefined index notices
- Confusing undefined index with undefined variable
- Assuming no error occurs when key missing
$_REQUEST, but prefer POST over GET and GET over COOKIE. Which code snippet correctly respects this priority?Solution
Step 1: Understand priority order requirement
The priority is POST > GET > COOKIE, so check in that order.Step 2: Analyze each option's order
if(isset($_POST['lang'])) { $lang = $_POST['lang']; } elseif(isset($_GET['lang'])) { $lang = $_GET['lang']; } elseif(isset($_COOKIE['lang'])) { $lang = $_COOKIE['lang']; } else { $lang = 'en'; } checks POST first, then GET, then COOKIE, assigning default 'en' if none set. $lang = $_REQUEST['lang'] ?? 'en'; uses $_REQUEST which merges but order depends on php.ini and is less explicit. Options C and D check in wrong orders.Final Answer:
Option A code snippet -> Option AQuick Check:
Explicit checks respect priority best [OK]
- Relying on $_REQUEST without knowing order
- Checking keys in wrong priority order
- Not providing a default value
Ready to prove it?
Pass the test and this pattern moves unknown
