What are the potential pitfalls of using functions like in_array() and substr() in PHP, and how can developers avoid them?

Using functions like in_array() and substr() in PHP can lead to potential pitfalls such as unexpected results or errors if not used correctly. To avoid these issues, developers should always check the return value of these functions and handle edge cases appropriately to ensure the desired outcome.

// Example of handling potential pitfalls with in_array() function
$haystack = ['apple', 'banana', 'cherry'];
$needle = 'apple';

if (in_array($needle, $haystack)) {
    echo "$needle is in the array";
} else {
    echo "$needle is not in the array";
}