What potential pitfalls should be avoided when using substr() and explode() functions in PHP?

One potential pitfall when using substr() and explode() functions in PHP is not checking if the functions return the expected results. It's important to handle cases where substr() returns false or explode() returns an empty array to prevent errors in your code. To avoid this issue, you can use conditional statements to check the return values before further processing the data.

$string = "Hello, World!";
$substring = substr($string, 0, 5);

if($substring !== false) {
    // continue processing the substring
    echo $substring;
} else {
    // handle the case where substr() returns false
    echo "Error: Unable to extract substring";
}

$array = explode(", ", $string);

if(!empty($array)) {
    // continue processing the array
    print_r($array);
} else {
    // handle the case where explode() returns an empty array
    echo "Error: Unable to explode string";
}