This example is just a simple PHP script to calculate two (2) values based on the selected operator. The sample output is posted below for illustration.
Output
HTML and PHP Code
<form method="post">
<input type="number" name="first">
<select name="operator">
<option>+</option>
<option>-</option>
<option>*</option>
<option>/</option>
</select>
<input type="number" name="second">
<input type="submit" name="calc" value="Calculate">
</form>
<?php
if(isset($_POST['calc']))
{
$first=$_POST['first'];
$second=$_POST['second'];
$op=$_POST['operator'];
if($op=="+")
$answer=$first+$second;
if($op=="-")
$answer=$first-$second;
if($op=="*")
$answer=$first*$second;
if($op=="/")
$answer=$first/$second;
echo $answer;
}
?>
You need to used a conditional statement to be able to perform certain operation such as addition, subtraction, multiplication and division. Hope you can create your own calculator in php.
0 comments:
Post a Comment