I have made a simple calculator using php, and i want to handle exception of division by zero, I am new at this and need help how to do this, if anyone can help:- Below is my code
<?php
$result = 0;
class calculator
{
var $a;
var $b;
function checkoperation($operator)
{
switch ($operator) {
case 'divide':
return $this->a / $this->b;
break;
case 'multiply':
return $this->a * $this->b;
break;
case 'add':
return $this->a + $this->b;
break;
case 'subtract':
return $this->a - $this->b;
break;
}
}
function getresult($a, $b, $c)
{
$this->a = $a;
$this->b = $b;
return $this->checkoperation($c);
}
}
$cal = new calculator();
if (isset($_POST['calculate_btn'])) {
$first_num = $_POST['first_num_txtbox'];
$second_num = $_POST['second_num_txtbox'];
$operation = $_POST['operation_slctbox'];
$result = $cal->getresult($first_num, $second_num, $operation);
echo "The result is: " . $result;
}
?>