Basic Calculator in PHP


This is a very simple task, But it is really good to all you newbies out there.

PHP Code
  1. <?php
  2. if (!isset($_POST['submit']) && !isset($_POST['submit1']) && !isset($_POST['submit2']) && !isset($_POST['submit3'])) {
  3. ?>
  4. <!DOCTYPE html>
  5. <html>
  6. <head>
  7. <title>Zayne's Calculator</title>
  8. </head>
  9. <body>
  10. <h1>Zayne's Calculator</h1>
  11. <p>Welcome to the calculator version 2. I hope you can find any sums you want. Thank you.</p>
  12. <form method="post">
  13. <input type="text" name="text" placeholder="Enter first number">
  14. <input type="text" name="text1" placeholder="Enter second number">
  15. <br><br>
  16. <input type="submit" name="submit" value="Add">
  17. <input type="submit" name="submit1" value="Minus">
  18. <input type="submit" name="submit2" value="Divide">
  19. <input type="submit" name="submit3" value="Multiply">
  20. </form>
  21. </body>
  22. </html>
  23. <?php
  24. } else {
  25. $num1 = isset($_POST['text']) ? $_POST['text'] : 0; // if not set, set 0
  26. $num2 = isset($_POST['text1']) ? $_POST['text1'] : 0; // if not set, set 0
  27.  
  28. if (!is_numeric($num1) || !is_numeric($num2)) { // Check inputs are numbers
  29. echo "<p>Please enter valid numeric values.</p>";
  30. echo "<a href='calculator.php'>Back</a>";
  31. exit;
  32. }
  33.  
  34. if (isset($_POST['submit'])) {
  35. $result = $num1 + $num2; // Addition
  36. echo "<p>$num1 + $num2 = $result</p>";
  37. } elseif (isset($_POST['submit1'])) {
  38. $result = $num1 - $num2; // Subtraction
  39. echo "<p>$num1 - $num2 = $result</p>";
  40. } elseif (isset($_POST['submit2'])) {
  41. if ($num2 == 0) { // Impossible
  42. echo "<p>Division by zero is not allowed.</p>";
  43. } else {
  44. $result = $num1 / $num2; // Division
  45. echo "<p>$num1 / $num2 = $result</p>";
  46. }
  47. } elseif (isset($_POST['submit3'])) {
  48. $result = $num1 * $num2; // Multiplication
  49. echo "<p>$num1 * $num2 = $result</p>";
  50. }
  51.  
  52. echo "<a href='index.php'>Back</a>";
  53. }
  54. ?>



Notice I did not put $_POST['example'], I just did it the short way by putting a $ symbol in from of the html name. This can be done, However it is more secure to use $_POST.
In php, It is basically just a language that interacts with forms. You put the value of a textbox into a variable and php reads it from there. You can then insert anything you want.
An example of sticking a textbox into a variable is. <input type=text name=text> This will be $_POST['text']. Lots of beginners battle with that. From there when i converted those to variables i used alot of operators, which are used to transform the values of the variables.
Nyaz's Avatar
Author:
Views:
2,340
Rating:
Posted on Monday 15th October 2007 at 07:53 PM
Arudis
Arudis's Avatar
put this script anywhere you want the calculator you show lol
Posted on Monday 15th October 2007 at 05:55 PM
rizwan1980pat
rizwan1980pat's Avatar
WHERE TO ENTER THIS SCRIPTS, SO THAT WE CAN MAKE USE OF IT.