Blogger Information
Blog 19
fans 0
comment 0
visits 10092
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
封装数组函数和实现简单的计算器功能
bloght5386
Original
643 people have browsed it

1.给定一个数组$arr = [23,3,45,6,78,8,34],筛选其偶数成员组成新的数组返回,请封装函数。

  1. <?php
  2. //1.给定一个数组$arr = [23,3,45,6,78,8,34],筛选其偶数成员组成新的数组返回,请封装函数。
  3. $arr = [23,3,45,6,78,8,34];
  4. function getArr($arr){
  5. $arr1 = array();
  6. foreach($arr as $k=>$v){
  7. if($v%2==0){
  8. $arr1[$k] = $v;
  9. }
  10. }
  11. return $arr1;
  12. }
  13. echo '<pre>';
  14. print_r(getArr($arr));

2. 尝试实现简单的计算器功能,语言不限制。

  1. 界面
  2. <!DOCTYPE html>
  3. <html lang="en">
  4. <head>
  5. <meta charset="UTF-8">
  6. <title>计算器</title>
  7. </head>
  8. <body>
  9. <form action="4.php" method="post">
  10. <span>
  11. <label for="first">第一个数</label>
  12. <input type="text" name="first" id="first"/>
  13. </span>
  14. <span>
  15. <select name="fh" id="">
  16. <option value="+">+</option>
  17. <option value="-">-</option>
  18. <option value="*">*</option>
  19. <option value="/">/</option>
  20. </select>
  21. </span>
  22. <span>
  23. <label for="second">第二个数</label>
  24. <input type="text" name="second" id="second"/>
  25. </span>
  26. <span>=</span>
  27. <span>
  28. <input type="text" value="<?php echo $res ?>">
  29. </span>
  30. <span>
  31. <input type="submit" value="计算">
  32. </span>
  33. </form>
  34. </body>
  35. </html>
  36. php代码功能:
  37. $first = $_POST['first']?? 0;
  38. $second = $_POST['second']?? 0;
  39. $fh = $_POST['fh']?? 0;
  40. switch($fh){
  41. case '+':
  42. $res = $first + $second;
  43. break;
  44. case '-':
  45. $res = $first - $second;
  46. break;
  47. case '*':
  48. $res = $first * $second;
  49. break;
  50. case '/':
  51. $res = $first / $second;
  52. break;
  53. }
  54. include_once ("./jsq.html");
Correcting teacher:灭绝师太灭绝师太

Correction status:qualified

Teacher's comments:计算器功能可以完善一下, 比如用户没有输入第一个数,或者除法运算第二个数为0
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