Simple Calculator Using PHP

Language

This is a simple Calculator that you can use in your project or systems. Just download the source code and follow the instructions below.

DIRECTIONS

HTML CODE

  1. <table style="border:2px solid #000;">
  2. <tr>
  3. <td style="background-color:lightblue; color:#000; font-family:'Times New Roman'">Enter First Number</td>
  4. <td colspan="1">
  5.  
  6. <input name="fvalue" type="number" style="color:blue"/></td>
  7. <tr>
  8. <td style="color:#000; font-family:'Times New Roman'">Select Operator</td>
  9. <td>
  10. <select name="operator" style="width: 63px">
  11. </select></td>
  12. </tr>
  13. <tr>
  14. <td style="background-color:lightblue; color:#000; font-family:'Times New Roman'">Enter First Number</td>
  15. <td class="auto-style5">
  16. <input name="lvalue" type="number" style="color:blue;"/></td>
  17.  
  18. </tr>
  19. <tr>
  20. <td></td>
  21. <td><input type="submit" name="calculate" value="Calculate" style="color:wheat;background-color:red;" /></td>
  22.  
  23. </tr>
  24. <tr>
  25. <td style="background-color:lightblue;color:#000;">Output = </td>
  26. <td style="color:darkblue"><?php echo $res;?></td>
  27.  
  28. </tr>
  29. </table>
  30. </form>

PHP CODE

  1. <?php
  2. ini_set('display_errors',0);
  3. if( isset( $_REQUEST['calculate'] ))
  4. {
  5. $operator=$_REQUEST['operator'];
  6. if($operator=="+")
  7. {
  8. $add1 = $_REQUEST['fvalue'];
  9. $add2 = $_REQUEST['lvalue'];
  10. $res= $add1+$add2;
  11. }
  12. if($operator=="-")
  13. {
  14. $add1 = $_REQUEST['fvalue'];
  15. $add2 = $_REQUEST['lvalue'];
  16. $res= $add1-$add2;
  17. }
  18. if($operator=="*")
  19. {
  20. $add1 = $_REQUEST['fvalue'];
  21. $add2 = $_REQUEST['lvalue'];
  22. $res =$add1*$add2;
  23. }
  24. if($operator=="/")
  25. {
  26. $add1 = $_REQUEST['fvalue'];
  27. $add2 = $_REQUEST['lvalue'];
  28. $res= $add1/$add2;
  29. }
  30. if($_REQUEST['fvalue']==NULL && $_REQUEST['lvalue']==NULL)
  31. {
  32. echo "<script language=javascript> alert(\"Please Enter values.\");</script>";
  33. }
  34. else if($_REQUEST['fvalue']==NULL)
  35. {
  36. echo "<script language=javascript> alert(\"Please Enter First value.\");</script>";
  37. }
  38. else if($_REQUEST['lvalue']==NULL)
  39. {
  40. echo "<script language=javascript> alert(\"Please Enter second value.\");</script>";
  41. }
  42. }
  43. ?>
For more information and inquiries, feel free to comment below or email me at [email protected]. May this simple calculator may help you in your projects or systems.

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment