+2 votes
in Programming Languages by (11.2k points)

In python, if we have a list of values, we can check whether or not a particular value is present in the list using 'in' [e.g. if val in list]. Is there an equivalent syntax in PHP?

1 Answer

0 votes
by (3.7k points)

Yes, there is an equivalent syntax in PHP. You can use in_array

Syntax is -

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

Example

<?php
$mylist = array("Mac", "Windows", "Android", "Ubuntu");
if (in_array("Mac", $mylist)) {
    echo "Yes";
}else{

echo "No";

}

?>

...