Blogger Information
Blog 20
fans 0
comment 1
visits 13141
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
php 简易计算器
zg的php学习
Original
1099 people have browsed it

php 简易计算器

  1. <?php
  2. $num1 = $_GET['num1'];
  3. $num2 = $_GET['num2'];
  4. $opt = $_GET['opt'];
  5. $result = 0;
  6. if(!is_numeric($num1)){
  7. $result = '请输入数字1';
  8. }else if(!is_numeric($num2)){
  9. $result = '请输入数字2';
  10. }else{
  11. switch($opt){
  12. case '+':
  13. $result = $num1 + $num2;
  14. break;
  15. case '-':
  16. $result = $num1 - $num2;
  17. break;
  18. case '*':
  19. $result = $num1 * $num2;
  20. break;
  21. case '/':
  22. if($num2 == '0'){
  23. $result = '除数不能为0 !';
  24. }else
  25. {
  26. $result = $num1 / $num2;
  27. }
  28. break;
  29. case '%':
  30. if($num2 == '0'){
  31. $result = '除数不能为0 !';
  32. }else
  33. {
  34. $result = $num1 % $num2;
  35. }
  36. break;
  37. default:
  38. $result = '无效运算!';
  39. };
  40. }
  41. ?>
  42. <!DOCTYPE html>
  43. <html lang="en">
  44. <head>
  45. <meta charset="UTF-8">
  46. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  47. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  48. <title>Document</title>
  49. </head>
  50. <body>
  51. <form action="" method="get" style="width:20em;display:grid;grid-template-columns:30% 70%;gap:1em;">
  52. <label>数字1:</label><input type="number" name="num1" value="<?= isset($_GET['num1'])?$num1 :'' ?>">
  53. <label>运算符:</label>
  54. <select name="opt" id="">
  55. <option value="+" <?= ($opt == '+') ? 'selected':'' ?> >+</option>
  56. <option value="-" <?= ($opt == '-') ? 'selected':'' ?> >-</option>
  57. <option value="*" <?= ($opt == '*') ? 'selected':'' ?> >*</option>
  58. <option value="/" <?= ($opt == '/') ? 'selected':'' ?> >/</option>
  59. <option value="%" <?= ($opt == '%') ? 'selected':'' ?> >%</option>
  60. </select>
  61. <label>数字2:</label><input type="number" name="num2" value="<?= isset($_GET['num2']) ?$num2 : '' ?>">
  62. <label>结 果:</label><input type="text" value = "<?= $result ?>">
  63. <label></label><input type="submit" value="提交">
  64. </form>
  65. </body>
  66. </html>

运行结果:
zy1

zy2

Correcting teacher:PHPzPHPz

Correction status:qualified

Teacher's comments:
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