How can PHP functions be modified to include return values for better output control and error handling?
To include return values for better output control and error handling in PHP functions, you can modify the function to return specific values based on the success or failure of the operation. This allows you to handle errors more effectively and provide meaningful feedback to the caller of the function.
function divide($numerator, $denominator) {
if ($denominator == 0) {
return "Error: Division by zero";
}
$result = $numerator / $denominator;
return $result;
}
// Example usage
$result = divide(10, 2);
if (is_numeric($result)) {
echo "Result: " . $result;
} else {
echo $result;
}