What are the advantages of using in_array() over preg_match() for checking user input against predefined values in PHP?
When checking user input against predefined values in PHP, using in_array() is generally more efficient and straightforward than using preg_match(). in_array() specifically checks if a value exists in an array, while preg_match() is used for pattern matching with regular expressions. In this scenario, where we are simply checking for the presence of a value in a list, in_array() is the more suitable choice.
// Example of using in_array() to check user input against predefined values
$allowed_values = array('apple', 'banana', 'orange');
$user_input = 'banana';
if (in_array($user_input, $allowed_values)) {
echo 'User input is valid.';
} else {
echo 'Invalid input.';
}