In PHP you may find lot of mathematical functions.Here, I am not going to cover all of them but very important mathematical fictions are covered with the practical examples in this tutorial.
Calculator interface is bellow.
Number 1 | ||
Number 2 | ||
Operation:- | Addition | Subtraction |
Division | Multiplication | |
Module | Power | |
Random Number | Percentage | |
Inserting the numbers in to the fields number1 and number2 then clicking operation radio button and pressing calculate you can get the values calculated. Codes for the above calculator is as bellow.
<table bgcolor=#d38a2a> <form name="calc" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <tr bgcolor=#ffffff><td>Number 1</td><td><input type="text" name="value1"><td rowspan=2 align="center"><img src="cal.png"></td></tr> <tr bgcolor=#ffffff><td>Number 2</td><td><input type="text" name="value2"></td></tr> <tr bgcolor=#ffffff><td>Operation:-</td><td><input type="radio" name="oper" value="add">Addition</td><td><input type="radio" name="oper" value="subtract">Subtraction</td></tr> <tr bgcolor=#ffffff><td> </td><td><input type="radio" name="oper" value="divide">Division</td><td><input type="radio" name="oper" value="multify">Multipication</td></tr> <tr bgcolor=#ffffff><td> </td><td><input type="radio" name="oper" value="mod">Module</td><td><input type="radio" name="oper" value="pow">Power</td></tr> <tr bgcolor=#ffffff><td> </td><td><input type="radio" name="oper" value="rad">Random Number</td><td><input type="radio" name="oper" value="per">Percentage</input></td></tr> <tr bgcolor=#ffffff><td colspan=3 align="right"><input type="submit" value="Calculate"></td></tr> </form> </table> <?php $number1=$_POST['value1']; $number2=$_POST['value2']; $oper=$_POST['oper']; if($oper=='add'){ $result=$number1+$number2; echo "Result is".$result; } else if($oper=='subtract'){ $result=$number1-$number2; echo "Result is".$result; } else if($oper=='divide'){ $result=$number1/$number2; echo "Result is".$result; } else if($oper=='multify'){ $result=$number1*$number2; echo "Result is".$result; } else if($oper=='mod'){ echo fmod($number1,$number2); } else if($oper=='pow'){ echo pow($number1,$number2); } else if($oper=='rad'){ echo rand($number1,$number2); } else if($oper=='per'){ $number3=$number2/100; $result=$number1*$number3; echo $result; } else { echo "<font color='red'>Wrong Input or Method</font>"; } ?> |
No comments:
Post a Comment