+2 votes
in Programming Languages by (71.8k 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

+1 vote
by (56.5k points)
selected by
 
Best answer

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";

}

?>


...