How can the use of in_array() in PHP lead to unexpected results in certain scenarios?

The use of in_array() in PHP can lead to unexpected results when comparing values that are loosely typed. This is because in_array() uses loose comparison (==) by default, which can lead to unexpected type conversions. To solve this issue, you can use the strict comparison operator (===) to ensure both the value and type match.

// Using strict comparison operator to avoid unexpected results
if (in_array($needle, $haystack, true)) {
    // Value exists in array
} else {
    // Value does not exist in array
}