if (is_error($my_variable)) { echo "There was an error with the variable: " . $my_variable->getMessage(); }
function divide($numerator, $denominator) { if ($denominator == 0) { $error = new Exception("Division by zero is not allowed"); return $error; } else { return $numerator / $denominator; } } $result = divide(10, 0); if (is_error($result)) { echo "Oops! There was an error: " . $result->getMessage(); } else { echo "The result is: " . $result; }In this example, we have a function called "divide" that takes two arguments and returns the result of dividing them. If the denominator is zero, we create an error using the "Exception" class and return it instead of the result. We then call the function with arguments that will cause an error. Finally, we use "is_error" to check whether the result is an error or not, and print out the appropriate message.