我创建了一个演示计算器,它完美运行,除了有一次提交表单时它进入 try 块,其余的 html 页面没有显示..我做错了吗?我看不出它不应该执行其余代码的任何理由
我非常理解 try catch finally 概念,但看不到错误在哪里
这是我的 class.calculator.php
class NoNumberProvided_Exception extends Exception {}
class Calculator {
function __construct() {
$args = func_get_args();
if(!$args) {
throw new NoNumberProvided_Exception("Please provide atleast 2 numbers");
} else {
if($args[0] && $args[1]) {
if($args[2] == "Add") {
echo $args[0]+$args[1];
} else if($args[2] == "Divide") {
echo $args[0]/$args[1];
} else if($args[2] == "Subtract") {
echo $args[0]-$args[1];
} else if($args[2] == "Multiply") {
echo $args[0]*$args[1];
}
} else {
throw new NoNumberProvided_Exception("Please provide atleast 2 numbers");
}
}
}
}
PHP:
if(isset($_POST['submit'])) {
include 'class.calculator.php';
try {
$num = new Calculator($_POST['number1'], $_POST['number2'], $_POST['submit']);
echo $num; // after the form gets submitted, this gets echoed but the html form below doesnt show on the page
} catch (NoNumberProvided_Exception $nonumber) {
echo $nonumber->getMessage();
}
}
HTML:
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
Number1: <input type="text" name="number1" id="number1" />
<br/>
Number2: <input type="text" name="number2" id="number2" />
<br/><br/>
<input type="submit" id="submit" name="submit" value="Add" />
<input type="submit" id="submit" name="submit" value="Divide" />
<input type="submit" id="submit" name="submit" value="Subtract" />
<input type="submit" id="submit" name="submit" value="Multiply" />
</form>
慕尼黑8549860