Blogger Information
Blog 13
fans 1
comment 0
visits 9913
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
php写一个计算器
大宇
Original
1639 people have browsed it
  1. <?php
  2. $result;
  3. $num1 = isset($_GET['num1']) ? $_GET['num1'] : 0;
  4. $num2 = isset($_GET['num2']) ? $_GET['num2'] : 0;
  5. $cal = isset($_GET['cal'])? $_GET['cal'] : 0;
  6. switch ($cal) {
  7. case '+':
  8. $result = $num1+$num2;
  9. break;
  10. case '-':
  11. $result = $num1-$num2;
  12. break;
  13. case '*':
  14. $result = $num1 * $num2;
  15. break;
  16. case '/':
  17. if ($num2 != 0) {
  18. $result = $num1 / $num2;
  19. } else {
  20. echo "被除数不可以为0";
  21. }
  22. break;
  23. }
  24. ?>
  25. <!DOCTYPE html>
  26. <html lang="en">
  27. <head>
  28. <meta charset="UTF-8">
  29. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  30. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  31. <title>计算器</title>
  32. </head>
  33. <body>
  34. <h2>计算器</h2>
  35. <form action="" method="get">
  36. 请输入第一个数<input type="text" name="num1"
  37. value="<?php echo $num1 ?>"><br>
  38. <input type="radio" name="cal" value="+" <?php if ($cal=="+") {
  39. echo "checked";
  40. }?>>+
  41. <input type="radio" name="cal" value="-" <?php if ($cal=="-") {
  42. echo "checked";
  43. }?>>-
  44. <input type="radio" name="cal" value="*" <?php if ($cal=="*") {
  45. echo "checked";
  46. }?>>*
  47. <input type="radio" name="cal" value="/" <?php if ($cal=="/") {
  48. echo "checked";
  49. }?>>/<br>
  50. 请输入第二个数 <input type="text" name="num2"
  51. value="<?php echo $sum2 ?>">
  52. <br>
  53. <button>提交</button>
  54. </form>
  55. <?php
  56. echo "<h6>$num1 {$cal} $num2 = {$result}</h6>";
  57. ?>
  58. </body>
  59. </html>

Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post