Arithmatic Calculator in PHP

by Yogesh Khanna 09-Jul-16

35 0 623

Now, you can easily create an arithmetic calculator in PHP for performing calculation related work in PHP.

Following is the PHP code of "Calculator" in which you can implement basic Arithmetic operations on the button i.e. Addition, multiplication, subtraction, and division with the help of operators.

Example:

<!Doctype html>
<html>

<head>
<meta charset="utf-8">
<title> Arithmatic Calculator </title>
</head>

<body>

<p align="center">
Arithmatic Calculator (Plus / Minus / Multiply / Division)
</p>

<br>
<?php
@$first=$_POST['first'];
@$second=$_POST['second'];
if(isset($_POST['plus']))
{
$plus=$first+$second;
echo '<table align="center">
<tr>
<td style="color:red;">Answer is: &nbsp;&nbsp;&nbsp;</td>
<td><input type="text" value="'.$plus.'" readonly/></td>
</tr>
</table>';
}
else if(isset($_POST['minus']))
{
$minus=$first-$second;
echo '<table align="center">
<tr>
<td style="color:red;">Answer is: &nbsp;&nbsp;&nbsp;</td>
<td><input type="text" value="'.$minus.'" readonly/></td>
</tr>
</table>';
}
else if(isset($_POST['multiply']))
{
$multiply=$first*$second;
echo '<table align="center">
<tr>
<td style="color:red;">Answer is: &nbsp;&nbsp;&nbsp;</td>
<td><input type="text" value="'.$multiply.'" readonly/></td>
</tr>
</table>';
}
else if(isset($_POST['divide']))
{
$divide=$first/$second;
echo '<table align="center">
<tr>
<td style="color:red;">Answer is: &nbsp;&nbsp;&nbsp;</td>
<td><input type="text" value="'.$divide.'" readonly/></td>
</tr>
</table>';
}
?>

<form action="" method="post">
<table align="center">
<tr>
<td> First Value </td>
<td><input type="text" name="first" /></td>
</tr>
<tr>
<td> Second Value </td>
<td><input type="text" name="second" /></td>
</tr>
</table>
<table align="center">
<tr>
<td><input type="submit" name="plus" value="+"></td>
<td><input type="submit" name="minus" value="-"></td>
<td><input type="submit" name="multiply" value="*"></td>
<td><input type="submit" name="divide" value="/"></td>
</tr>
</table>
</form>

</body>
</html>

Result:

Arithmatic Calculator in PHP



Share:

Comments

Leave a Reply