Blogger Information
Blog 14
fans 0
comment 0
visits 9501
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP简易计算器&封装函数练习
Mr.Ran
Original
817 people have browsed it

PHP简易计算器

实现思路:
1.运用JS处理页面操作逻辑、收集必要的计算参数;
2.将参数传给PHP后端处理并返回结果;
3.将结果展示到前端页面。

PHP代码

  1. <?php
  2. @$n1 = $_GET["n1"];
  3. @$n2 = $_GET["n2"];
  4. @$f = $_GET["fu"];
  5. //计算函数
  6. function calc($n_1,$n_2,$fu){
  7. if(empty($n_1) || empty($n_2) || empty($fu)){
  8. return "";
  9. }else{
  10. $r = 0;
  11. switch ($fu) {
  12. case 'and':
  13. $r = $n_1 + $n_2;
  14. break;
  15. case 'minus':
  16. $r = $n_1 - $n_2;
  17. break;
  18. case 'times':
  19. $r = $n_1 * $n_2;
  20. break;
  21. case 'into':
  22. $r = $n_1 / $n_2;
  23. break;
  24. default:
  25. $r = 0;
  26. break;
  27. }
  28. return $r;
  29. }
  30. }
  31. ?>

HTML代码

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  7. <title>计算器(PHP版)</title>
  8. <link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.6.0/css/bootstrap.css" rel="stylesheet">
  9. <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js"></script>
  10. <style>.row{padding:12px;}</style>
  11. </head>
  12. <body style="background:#313131;color:#9b9b9b;">
  13. <div class="container clear" style="padding-top:20px; padding-bottom:20px;">
  14. <div class="row">
  15. <div class="col-12">计算器(PHP版)</div>
  16. </div>
  17. <div class="row">
  18. <div class="col-12"><input type="text" id="input" value="<?= calc($n1,$n2,$f) ?>" class="form-control" style="text-align:right;"></div>
  19. </div>
  20. <div class="row">
  21. <div class="col-3"><button onclick="getBtn(7)" type="button" class="btn btn-lg btn-block btn-secondary">7</button></div>
  22. <div class="col-3"><button onclick="getBtn(8)" type="button" class="btn btn-lg btn-block btn-secondary">8</button></div>
  23. <div class="col-3"><button onclick="getBtn(9)" type="button" class="btn btn-lg btn-block btn-secondary">9</button></div>
  24. <div class="col-3"><button onclick="getBtn('+')" type="button" class="btn btn-lg btn-block btn-danger">+</button></div>
  25. </div>
  26. <div class="row">
  27. <div class="col-3"><button onclick="getBtn(4)" type="button" class="btn btn-lg btn-block btn-secondary">4</button></div>
  28. <div class="col-3"><button onclick="getBtn(5)" type="button" class="btn btn-lg btn-block btn-secondary">5</button></div>
  29. <div class="col-3"><button onclick="getBtn(6)" type="button" class="btn btn-lg btn-block btn-secondary">6</button></div>
  30. <div class="col-3"><button onclick="getBtn('-')" type="button" class="btn btn-lg btn-block btn-warning">-</button></div>
  31. </div>
  32. <div class="row">
  33. <div class="col-3"><button onclick="getBtn(1)" type="button" class="btn btn-lg btn-block btn-secondary">1</button></div>
  34. <div class="col-3"><button onclick="getBtn(2)" type="button" class="btn btn-lg btn-block btn-secondary">2</button></div>
  35. <div class="col-3"><button onclick="getBtn(3)" type="button" class="btn btn-lg btn-block btn-secondary">3</button></div>
  36. <div class="col-3"><button onclick="getBtn('*')" type="button" class="btn btn-lg btn-block btn-info">×</button></div>
  37. </div>
  38. <div class="row">
  39. <div class="col-3"><button onclick="getBtn('.')" type="button" class="btn btn-lg btn-block btn-secondary">.</button></div>
  40. <div class="col-3"><button onclick="getBtn(0)" type="button" class="btn btn-lg btn-block btn-secondary">0</button></div>
  41. <div class="col-3"><button onclick="getBtn('=')" type="button" class="btn btn-lg btn-block btn-success">=</button></div>
  42. <div class="col-3"><button onclick="getBtn('/')" type="button" class="btn btn-lg btn-block btn-primary">÷</button></div>
  43. </div>
  44. <div style="text-align:center;margin-top:20px;color:#9b9b9b;font-size:12px;">©2021 CopyRight PHP中文网</div>
  45. </div>
  46. </body>
  47. </html>

JS代码

  1. <script>
  2. var n1 = "";//第一个数
  3. var n2 = "";//第二个数
  4. var n3 = "";//当前数
  5. var v3 = "";//计算结果
  6. var fu = "";//运算符号
  7. var bl = false;
  8. var view = $("#input"); //显示屏
  9. var view_val = "";
  10. //按钮事件
  11. function getBtn(v){
  12. //检查v是否为符号
  13. if ((typeof v=='number')&&v.constructor==Number|| v == ".") {
  14. //拼接数字字符串
  15. n3 += v.toString().concat();
  16. //赋值给第二个数
  17. if (bl){n2 = n3;}
  18. } else {
  19. if (v == "=") {
  20. if (n1 != "" && n2 !="") {
  21. //运算符号赋值;
  22. var ff;
  23. switch (fu) {
  24. case '+':
  25. ff = "and";
  26. break;
  27. case '-':
  28. ff = "minus";
  29. break;
  30. case '*':
  31. ff = "times";
  32. break;
  33. case '/':
  34. ff = "into";
  35. break;
  36. default:
  37. alert("错误!");
  38. location.reload();
  39. break;
  40. }
  41. window.location.href="calc.php?n1="+n1+"&n2="+n2+"&fu="+ff+"";
  42. }else{
  43. alert("输入错误!");
  44. location.reload();
  45. }
  46. }else{
  47. fu = v;
  48. //赋值给第一个数
  49. if (n1=="") {
  50. n1 = n3;
  51. n3 = "";
  52. bl = true;
  53. }
  54. }
  55. }
  56. //输出显示屏;
  57. if (v != '=') {
  58. view_val+=v;
  59. view.val(view_val);
  60. }
  61. }
  62. </script>

效果展示


取偶函数练习

  1. <?php
  2. $arr = [23,3,45,6,78,8,34,5,6,48];
  3. //调用取偶函数
  4. print_r(getOdd($arr));
  5. //取偶函数
  6. function getOdd(array $a):array
  7. {
  8. $arr2=[];
  9. foreach ($a as $v) {
  10. if($v % 2 == 0){
  11. //将筛选的偶数赋值给新的数组
  12. array_push($arr2,$v);
  13. }
  14. }
  15. return $arr2;
  16. }
  17. ?>
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