Checking whether an element exists in array


Use the in_array() function. Learn how it work.

This is the schema:

bool in_array ( mixed $needle , array $haystack [, bool $strict = FALSE ] )

where:

$needle is the searched value

$haystack is the aray

$strict as TRUE (FALSE by default), impose the types correspondence between $needle and the found element in array

 

Look this code as exemple:

 

<?php
$os = array("Mac", "NT", "Irix", "Linux");
if (in_array("Irix", $os)) {
    echo "Got Irix";
}
if (in_array("mac", $os)) {
    echo "Got mac";
}
?>

Related articles