0
0
PHPprogramming~20 mins

$_REQUEST behavior in PHP - Practice Problems & Coding Challenges

Choose your learning style9 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.