Skip to content
PHP Logo

How can you check if a value is present in an array in PHP?

To check if a value is present in an array in PHP, you can use the in_array() function. This function takes two parameters: the value you want to check for and the array you want to search in. It returns true if the value is found in the array, and false otherwise.

php
$value = 5;
$array = [1, 2, 3, 4, 5];

if (in_array($value, $array)) {
    echo "Value is present in the array.";
} else {
    echo "Value is not present in the array.";
}