Hello everyone, I was practicing OOP PHP, and I ended up with an OOP PHP Calculator! So here is the source:
Copy paste the above code in a *.php file and save it. In-order to run it you need an Apache server. Hope you like the release! Please give me ideas to release something the next time because I am always looking to code something and release it! Please give me feed back if you have ideas on improving my code!
Code:
<?php
class Calculater {
public function add($num1, $num2) {
$total = $num1 + $num2;
echo $total;
}
public function substract($num1, $num2) {
$total = $num1 - $num2;
echo $total;
}
public function multiply($num1, $num2) {
$total = $num1 * $num2;
echo $total;
}
public function divide($num1, $num2) {
$total = $num1 / $num2;
echo $total;
}
}
$Calculate = new Calculater;
if(isset($_REQUEST['submit'])) {
$num1 = $_POST['num1'];
$num2 = $_POST['num2'];
if(!ctype_digit($num1)) {
echo "Error: Your inputs may only contain numbers";
exit();
}
if(!ctype_digit($num2)) {
echo "Error: Your inputs may only contain numbers";
exit();
}
$operation = $_POST['operation'];
if($operation == "Add") {
$Calculate->add($num1, $num2);
}
if($operation == "Substract") {
$Calculate->substract($num1, $num2);
}
if($operation == "Multiply") {
$Calculate->multiply($num1, $num2);
}
if($operation == "Divide") {
$Calculate->divide($num1, $num2);
}
}
?>
<form method="post" action="" name="submit">
<input type="text" name="num1">
<input type="text" name="num2">
<select name="operation">
<option>Add</option>
<option>Substract</option>
<option>Multiply</option>
<option>Divide</option>
</select>
<input type="submit" name="submit" value="Submit">
</form>
Copy paste the above code in a *.php file and save it. In-order to run it you need an Apache server. Hope you like the release! Please give me ideas to release something the next time because I am always looking to code something and release it! Please give me feed back if you have ideas on improving my code!