Consider this PHP script receiving a GET request with URL parameters ?name=Alice&age=30. What will be the output?
<?php $_GET['name'] = 'Alice'; $_GET['age'] = '30'; $_POST = []; $_COOKIE = []; $_REQUEST = array_merge($_COOKIE, $_POST, $_GET); echo $_REQUEST['name'] . ' is ' . $_REQUEST['age'] . ' years old.'; ?>
Remember that $_REQUEST contains data from $_GET, $_POST, and $_COOKIE.
The $_REQUEST array includes all GET, POST, and COOKIE variables. Since $_GET has name and age, $_REQUEST will have them too. So the output is "Alice is 30 years old."
Given this PHP code snippet, what will be the output?
<?php $_GET['color'] = 'blue'; $_POST['color'] = 'red'; $_COOKIE = []; $_REQUEST = array_merge($_COOKIE, $_GET, $_POST); echo $_REQUEST['color']; ?>
Check the order PHP uses to populate $_REQUEST by default. Later sources override earlier ones.
By default, PHP populates $_REQUEST in the order of GPC (GET, POST, COOKIE), with later values overriding earlier ones. So the POST value 'red' overwrites the GET 'blue' in $_REQUEST. The output is 'red'.
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?
Think about how to safely check if an array key exists before using it.
Accessing $_REQUEST['user'] directly without checking if it exists causes a notice if the key is missing. Use isset($_REQUEST['user']) to avoid this.
Given this PHP code snippet:
<?php $_GET['session'] = 'get_value'; $_COOKIE['session'] = 'cookie_value'; $_POST = []; echo $_REQUEST['session']; ?>
What will it print?
Recall the default order of variables in $_REQUEST. Later sources override earlier.
PHP's default order for $_REQUEST is GET, POST, COOKIE, with later values overriding earlier ones. $_POST is empty, so COOKIE 'cookie_value' overrides GET 'get_value'. Output is 'cookie_value'.
PHP populates $_REQUEST using a default order of variables. Which PHP configuration directive controls this order?
Check php.ini settings related to request variables.
The request_order directive in php.ini controls the order in which GET, POST, and COOKIE variables populate $_REQUEST. If not set, variables_order is used as fallback.