What is the difference between using a blacklist and a whitelist approach for validating input in PHP?

When validating input in PHP, using a blacklist approach involves specifying what values or characters are not allowed, while a whitelist approach involves specifying what values or characters are allowed. It is generally considered safer to use a whitelist approach as it explicitly defines what is acceptable input, reducing the risk of allowing potentially harmful input.

// Whitelist approach for validating input in PHP
$allowed_values = array("red", "green", "blue");
$user_input = $_POST['color'];

if (in_array($user_input, $allowed_values)) {
    // Input is valid
    echo "Input is valid";
} else {
    // Input is not valid
    echo "Input is not valid";
}