PHP, How to catch a division by zero?

if ($baz == 0.0) {
    echo 'Divisor is 0';
} else {
    ...
}

Rather than use eval, which is highly dangerous if you’re using user-input within the evalled expression, why not use a proper parser such as evalmath on PHPClasses, and which raises a clean exception on divide by zero

Leave a Comment