PHP - Check if array contains a value

July 12, 2022  ‐ 1 min read

The in_array() function in PHP tells you whether a value is present in a given array. By default it does a loose comparison, meaning that it evaluates to true if you check whether a string "1" is present in an array with the number one: [1].

<?php

in_array(1, [0, 1]);
//=> true

in_array('1', [0, 1]);
//=> true

in_array(2, [0, 1]);
//=> false

We can do a strict comparison by passing true as a third argument to the in_array() function":

<?php

in_array('1', [0, 1]);
//=> false