Bird
Raised Fist0
PHPprogramming~20 mins

$_REQUEST behavior in PHP - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Master of $_REQUEST
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this PHP code using $_REQUEST?

Consider this PHP script receiving a GET request with URL parameters ?name=Alice&age=30. What will be the output?

PHP
<?php
$_GET['name'] = 'Alice';
$_GET['age'] = '30';
$_POST = [];
$_COOKIE = [];
$_REQUEST = array_merge($_COOKIE, $_POST, $_GET);
echo $_REQUEST['name'] . ' is ' . $_REQUEST['age'] . ' years old.';
?>
ANotice: Undefined index: name
BAlice is 30 years old.
C30 is Alice years old.
DEmpty output
Attempts:
2 left
💡 Hint

Remember that $_REQUEST contains data from $_GET, $_POST, and $_COOKIE.

❓ Predict Output
intermediate
2:00remaining
What happens if a POST and GET parameter have the same name in $_REQUEST?

Given this PHP code snippet, what will be the output?

PHP
<?php
$_GET['color'] = 'blue';
$_POST['color'] = 'red';
$_COOKIE = [];
$_REQUEST = array_merge($_COOKIE, $_GET, $_POST);
echo $_REQUEST['color'];
?>
Ared
Bblue
CNotice: Undefined index: color
DEmpty output
Attempts:
2 left
💡 Hint

Check the order PHP uses to populate $_REQUEST by default. Later sources override earlier ones.

🔧 Debug
advanced
2:00remaining
Why does this code produce an undefined index error for $_REQUEST?

Look at this PHP code snippet:

<?php
if ($_REQUEST['user']) {
    echo 'User is set';
} else {
    echo 'User is not set';
}
?>

When no parameters are sent, it throws a notice: Undefined index: user. Why?

ABecause $_REQUEST['user'] is accessed without checking if it exists first
BBecause $_REQUEST is always empty
CBecause PHP does not support $_REQUEST
DBecause the code should use $_POST instead
Attempts:
2 left
💡 Hint

Think about how to safely check if an array key exists before using it.

❓ Predict Output
advanced
2:00remaining
What is the output when $_COOKIE and $_GET have the same key in $_REQUEST?

Given this PHP code snippet:

<?php
$_GET['session'] = 'get_value';
$_COOKIE['session'] = 'cookie_value';
$_POST = [];
echo $_REQUEST['session'];
?>

What will it print?

ANotice: Undefined index: session
BEmpty output
Ccookie_value
Dget_value
Attempts:
2 left
💡 Hint

Recall the default order of variables in $_REQUEST. Later sources override earlier.

🧠 Conceptual
expert
2:00remaining
How can you change the order of variables in $_REQUEST?

PHP populates $_REQUEST using a default order of variables. Which PHP configuration directive controls this order?

Avariables_order
Bsuperglobals_order
Crequest_variables
Drequest_order
Attempts:
2 left
💡 Hint

Check php.ini settings related to request variables.

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

  1. Step 1: Understand the purpose of $_REQUEST

    $_REQUEST is designed to collect data from multiple sources: GET, POST, and COOKIE.
  2. Step 2: Identify what $_REQUEST includes

    It merges these three superglobals into one array for easier access.
  3. Final Answer:

    Data from GET, POST, and COOKIE combined -> Option A
  4. 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

  1. Step 1: Recall PHP array syntax

    PHP superglobals like $_REQUEST are associative arrays accessed with square brackets and quotes for keys.
  2. Step 2: Identify correct syntax for accessing 'username'

    The correct syntax is $_REQUEST['username']. Using arrows or dots is invalid for arrays.
  3. Final Answer:

    $_REQUEST['username'] -> Option D
  4. 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

  1. 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.
  2. Step 2: Determine which value $_REQUEST['color'] holds

    Since POST sends color=blue, it overrides the GET color=red. So, $_REQUEST['color'] is 'blue'.
  3. Final Answer:

    blue -> Option C
  4. 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

  1. Step 1: Identify the error from accessing missing key

    Accessing $_REQUEST['user'] when 'user' is not set causes a Notice: Undefined index.
  2. Step 2: Fix by checking if key exists

    Use isset($_REQUEST['user']) before accessing to avoid the notice.
  3. Final Answer:

    Notice: Undefined index; fix by checking with isset() -> Option B
  4. 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?
hard
A. if(isset($_POST['lang'])) { $lang = $_POST['lang']; } elseif(isset($_GET['lang'])) { $lang = $_GET['lang']; } elseif(isset($_COOKIE['lang'])) { $lang = $_COOKIE['lang']; } else { $lang = 'en'; }
B. $lang = $_REQUEST['lang'] ?? 'en';
C. $lang = $_COOKIE['lang'] ?? $_GET['lang'] ?? $_POST['lang'] ?? 'en';
D. $lang = $_GET['lang'] ?? $_POST['lang'] ?? $_COOKIE['lang'] ?? 'en';

Solution

  1. Step 1: Understand priority order requirement

    The priority is POST > GET > COOKIE, so check in that order.
  2. 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.
  3. Final Answer:

    Option A code snippet -> Option A
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard