I have created a demo calculator which works perfect except one time when the form gets submitted it goes inside try block and the rest html page doesn't get displayed.. am i doing it wrong? i dont see any reason why it shouldn't execute the rest of the code
I very well understand the try catch finally concept but cant see where's the error
this is my 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>
<?php echo 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(); } } ?>echo