Complete the code to access a value from the $_REQUEST superglobal.
<?php
$value = $_REQUEST[[1]];
echo $value;
?>$_REQUEST is an associative array that contains data from GET, POST, and COOKIE. You access values by their key as a string.
Complete the code to check if a key exists in $_REQUEST.
<?php if (isset($_REQUEST[[1]])) { echo "Key exists."; } else { echo "Key does not exist."; } ?>
Keys in $_REQUEST must be strings, so you need quotes around the key name when checking with isset().
Fix the error in the code to correctly retrieve a POST value using $_REQUEST.
<?php // Assume form method is POST $name = $_REQUEST[1]"name"]; echo $name; ?>
To access an element in an array, use square brackets []. Parentheses or arrows are incorrect for arrays.
Fill both blanks to create a filtered array from $_REQUEST with keys longer than 4 characters.
<?php
$filtered = array_filter($_REQUEST, function($key) {
return strlen($key) [1] 4;
}, [2]);
print_r($filtered);
?>Use > to check keys longer than 4 characters. Use ARRAY_FILTER_USE_KEY to apply the callback to keys.
Fill all three blanks to create a new array with uppercase keys and values from $_REQUEST where values are not empty.
<?php
$result = array_combine(
array_map([1], array_keys($_REQUEST)),
array_filter($_REQUEST, function($value) {
return $value [2] '';
})
);
print_r($result);
?>Use strtoupper to convert keys to uppercase. Use != to check values not equal to empty string.