Sunday, June 5, 2016

How To Code A Simple Calculator In PHP

In this php tutorial you will learn how to make a simple PHP calculator
<?php

if($submit)
{
if($operator == *)
{
echo $numa * $numb;
} elseif($operator == /)
{
echo $numa / $numb;
} elseif($operator == +)
{
echo $numa + $numb;
} elseif($operator == -)
{
echo $numa - $numb;
}
} else { ?>
<form method="POST" action="<?php $_SERVER[PHP_SELF]; ?>">
<input type="text" name="numa" size="10">
<select name="operator">
<option value="+">Add</option>
<option value="-">Subtract</option>
<option value="*">Multiply</option>
<option value="/">Divide</option>
</select>
<input type="text" name="numb" size="10">
<input type="submit" value="Calculate" name="submit">
</form>
<?php } ?>
If you have any suggestions regarding the script please feel free to share it with us.Feel free to edit the above script to suit your personal needs.

Subscribe to our blog and we will email you similar interesting php scripts as soon as it is published.

Beside you could also refer to our PHP Interview Questions Series.

Enjoy!