Blogger Information
Blog 7
fans 0
comment 0
visits 3971
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
php基础编程作业-0805
Lank的博客
Original
508 people have browsed it

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

  1. <?php
  2. $arr = [23,3,45,6,78,8,34];
  3. function odd(array $arr):array
  4. {
  5. $newArr=[];
  6. for($i=0;$i<count($arr);$i++){
  7. if($arr[$i]%2==0){
  8. $newArr[]=$arr[$i];
  9. //或者使用array_push($newArr,$arr[$i]);
  10. }
  11. }
  12. return $newArr;
  13. }
  14. var_dump(odd($arr));
  15. ?>

运行效果如下

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

  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.0">
  7. <title>计算器</title>
  8. </head>
  9. <body>
  10. <?php
  11. error_reporting(E_ALL &~E_NOTICE);
  12. if(isset($_POST['sub'])):
  13. if($_POST['opt']=='/'
  14. &&$_POST['num2']==0) $mess="
  15. <span style='color:red;'>除数不能为0</span>";
  16. endif;
  17. ?>
  18. <h1>计算器</h1>
  19. <table>
  20. <form action="" method="POST">
  21. <tr>
  22. <td>
  23. <input type="number" name="num1" required value="<?=$_POST['num1']?>">
  24. </td>
  25. <td>
  26. <select name="opt">
  27. <option value="+" <?=$_POST['opt']=="+"?"selected":"" ?>>+</option>
  28. <option value="-" <?=$_POST['opt']=="-"?"selected":"" ?>>-</option>
  29. <option value="*" <?=$_POST['opt']=="*"?"selected":"" ?>>*</option>
  30. <option value="/" <?=$_POST['opt']=="/"?"selected":"" ?>>/</option>
  31. </select>
  32. </td>
  33. <td>
  34. <input type="number" name="num2" required value="<?=$_POST['num2']?>">
  35. </td>
  36. <td>
  37. <input type="submit" name="sub" value="计算">
  38. <?php
  39. if(!$mess && isset($_POST['sub']) ):
  40. switch($_POST['opt']):
  41. case "+":
  42. $sum=(int)$_POST['num1']+(int)$_POST['num2'];break;
  43. case "-":
  44. $sum=(int)$_POST['num1']-(int)$_POST['num2'];break;
  45. case "*":
  46. $sum=(int)$_POST['num1']*(int)$_POST['num2'];break;
  47. case "/":
  48. $sum=(int)$_POST['num1']/(int)$_POST['num2'];break;
  49. endswitch;
  50. $res="计算结果:{$_POST['num1']}
  51. {$_POST['opt']}{$_POST['num2']}={$sum}";
  52. echo "<span style='color: green;'>{$res}</span>";
  53. else:
  54. echo $mess;
  55. endif;
  56. ?>
  57. </td>
  58. </tr>
  59. </form>
  60. </table>
  61. </body>
  62. </html>

运行效果如下:

Correcting teacher:天蓬老师天蓬老师

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