php
Create a Calculator with PHP
Create a Calculator with PHP
<body>
<?php
class calc {
var $number1;
var $number2;
function add($number1,$number2)
{
$result =$number1 + $number2;
echo("The sum of $number1 and $number2 is $result<br><br>");
echo("$number1 + $number2 = $result");
exit;
}
function subtract($number1,$number2)
{
$result =$number1 - $number2;
echo("The difference of $number1 and $number2 is $result<br><br>");
echo $result;
exit;
}
function divide($number1,$number2)
{
$result =$number1 / $number2;
echo("$number1 divided by $number2 is $result<br><br>");
echo("$number1 ÷ $number2 = $result");
exit;
}
function multiply($number1,$number2)
{
$result =$number1 * $number2;
echo("The product of $number1 and $number2 is $result<br><br>");
echo("$number1 x $number2 = $result");
exit;
}
}
$calc = new calc();
?>
<TITLE>PHP Calculator v1</TITLE>
<form name="calc" action="" method="POST">
Number 1: <input type=number name='value1'><br>
Number 2: <input type=number name='value2'><br>
Operation: <input type=radio name='oper' value="add">Addition <input type=radio name=oper value="subtract">Subtraction <input type=radio name=oper value="divide">Division <input type=radio name=oper value="multiply">Multiplication</input><br>
<input type=submit value="Calculate" name="submit">
</form>
<?php
if(isset($_POST['submit']))
{
$number1 = $_POST['value1'];
$number2 = $_POST['value2'];
$oper = $_POST['oper'];
if(!$number1){
echo("You must enter number 1!");
exit;
}
else
if(!$number2){
echo("You must enter number 2!");
exit;
} else
if(!$oper){
echo("You must select an operation to do with the numbers!");
exit;
}
if($oper == "add"){
$calc->add($number1,$number2);
}
if($oper == "subtract"){
$calc->subtract($number1,$number2);
}
if($oper == "divide"){
$calc->divide($number1,$number2);
}
if($oper == "multiply"){
$calc->multiply($number1,$number2);
}
}
?>
</body>
0 comments