📖 Scenario: You are building a simple PHP script that collects user input from different sources: URL parameters, form data sent via POST, and cookies. You want to understand how PHP's $_REQUEST superglobal works by combining these inputs.
🎯 Goal: Create a PHP script that sets up sample $_GET, $_POST, and $_COOKIE data, then uses $_REQUEST to access all these values together. Finally, display the combined results.
📋 What You'll Learn
Create arrays for $_GET, $_POST, and $_COOKIE with exact keys and values
Create a variable request_order that defines the order of $_REQUEST variables
Use $_REQUEST to combine all inputs according to request_order
Print the combined $_REQUEST array
💡 Why This Matters
🌍 Real World
Web developers often need to handle user input from multiple sources like URL parameters, form submissions, and cookies. Understanding $_REQUEST helps manage these inputs safely and predictably.
💼 Career
Knowing how to work with PHP superglobals like $_REQUEST is essential for backend web development, especially when building forms, handling sessions, and processing user data.
Progress0 / 4 steps
1
Setup $_GET, $_POST, and $_COOKIE arrays
Create the PHP arrays $_GET, $_POST, and $_COOKIE with these exact entries: $_GET = ['color' => 'red', 'size' => 'large']; $_POST = ['color' => 'blue', 'shape' => 'circle']; $_COOKIE = ['size' => 'medium', 'texture' => 'smooth'];
PHP
Hint
Use PHP array syntax to assign the exact keys and values to $_GET, $_POST, and $_COOKIE.
2
Define request_order variable
Create a variable called request_order and set it to the string 'GP' to specify that $_REQUEST should prioritize $_GET then $_POST variables.
PHP
Hint
Assign the string 'GP' to the variable $request_order.
3
Combine arrays into $_REQUEST according to request_order
Create the $_REQUEST array by combining $_GET, $_POST, and $_COOKIE according to the order in $request_order. Use array_merge in the order of $_GET then $_POST then $_COOKIE.
PHP
Hint
Use array_merge($_COOKIE, $_POST, $_GET) to combine the arrays in the order specified by $request_order.
4
Print the combined $_REQUEST array
Use print_r to display the contents of the $_REQUEST array.
PHP
Hint
Use print_r($_REQUEST); to show the combined array.
Practice
(1/5)
1. What does the PHP superglobal $_REQUEST contain?
easy
A. Data from GET, POST, and COOKIE combined
B. Only data sent via GET method
C. Only data sent via POST method
D. Only data stored in cookies
Solution
Step 1: Understand the purpose of $_REQUEST
$_REQUEST is designed to collect data from multiple sources: GET, POST, and COOKIE.
Step 2: Identify what $_REQUEST includes
It merges these three superglobals into one array for easier access.
Final Answer:
Data from GET, POST, and COOKIE combined -> Option A
Quick Check:
$_REQUEST = GET + POST + COOKIE [OK]
Hint: Remember: $_REQUEST mixes GET, POST, and COOKIE data [OK]
Common Mistakes:
Thinking $_REQUEST only contains GET or POST data
Confusing $_REQUEST with $_POST or $_GET
Assuming $_REQUEST excludes cookies
2. Which of the following is the correct way to access a form field named username using $_REQUEST in PHP?
easy
A. $_REQUEST->username
B. request['username']
C. $_REQUEST.username
D. $_REQUEST['username']
Solution
Step 1: Recall PHP array syntax
PHP superglobals like $_REQUEST are 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 D
Quick Check:
Array access uses brackets and quotes [OK]
Hint: Use square brackets with quotes for $_REQUEST keys [OK]
Common Mistakes:
Using object notation (->) instead of array syntax
Using dot notation like in JavaScript
Forgetting quotes around the key
3. Given the following PHP code, what will be the output if the URL is script.php?color=red and the form sends color=blue via POST?
<?php
print_r($_REQUEST['color']);
?>
medium
A. red
B. Array
C. blue
D. Undefined index error
Solution
Step 1: Understand the order of variables in $_REQUEST
By default, PHP's $_REQUEST uses the order: GET, POST, COOKIE. POST data overrides GET if keys are the same.
Step 2: Determine which value $_REQUEST['color'] holds
Since POST sends color=blue, it overrides the GET color=red. So, $_REQUEST['color'] is 'blue'.
Final Answer:
blue -> Option C
Quick Check:
POST overrides GET in $_REQUEST [OK]
Hint: POST data overrides GET in $_REQUEST by default [OK]
Common Mistakes:
Assuming GET value appears first
Confusing $_REQUEST with $_GET or $_POST
Expecting an array output
4. Consider this PHP snippet:
<?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?
medium
A. No error; prints empty string
B. Notice: Undefined index; fix by checking with isset()
C. Warning: Invalid argument; fix by casting to string
D. Fatal error: Undefined variable; fix by declaring variable
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
Use isset($_REQUEST['user']) before accessing to avoid the notice.
Final Answer:
Notice: Undefined index; fix by checking with isset() -> Option B
Quick Check:
Check keys with isset() to avoid notices [OK]
Hint: Always check isset() before accessing $_REQUEST keys [OK]
Common Mistakes:
Ignoring undefined index notices
Confusing undefined index with undefined variable
Assuming no error occurs when key missing
5. You want to safely get a user's preferred language from either GET, POST, or COOKIE using $_REQUEST, but prefer POST over GET and GET over COOKIE. Which code snippet correctly respects this priority?
C. $lang = $_COOKIE['lang'] ?? $_GET['lang'] ?? $_POST['lang'] ?? 'en';
D. $lang = $_GET['lang'] ?? $_POST['lang'] ?? $_COOKIE['lang'] ?? 'en';
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 A
Quick Check:
Explicit checks respect priority best [OK]
Hint: Check POST, then GET, then COOKIE explicitly for priority [OK]
Common Mistakes:
Relying on $_REQUEST without knowing order
Checking keys in wrong priority order
Not providing a default value
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.