【发布时间】:2017-02-28 03:33:00
【问题描述】:
我使用 php 制作了一个简单的计算器,我想处理除以零的异常,我是新手,需要帮助如何做到这一点,如果有人可以提供帮助:- 下面是我的代码
<?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;
}
?>
【问题讨论】:
标签: php exception-handling divide-by-zero