Name

in_array()

Synopsis

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

The in_array() function will return true if an array contains a specific value; otherwise, it will return false:

    $needle = "Sam";
    $haystack = array("Johnny", "Timmy", "Bobby", "Sam", "Tammy", "Joe");

    if (in_array($needle, $haystack)) {
            print "$needle is in the array!\n";
    } else {
            print "$needle is not in the array\n";
    }

There is an optional boolean third parameter for in_array() (set to false by default) that defines whether you want to use strict checking or not. If parameter three is set to true, PHP will return true only if the value is in the array and of the same type—that is, if they are identical in the same way as the = = = operator (three equals signs).

Get PHP in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.