Blogger Information
Blog 6
fans 0
comment 0
visits 2920
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP基础课第四次作业
阿远
Original
499 people have browsed it

0806作业

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

  1. <?php
  2. $arr = [23,1,25,7,23,7,12];
  3. $arr7=[];
  4. function arr($arr,$arr7)
  5. {
  6. for ($i = 0; $i <count($arr);$i++){
  7. //print($arr[$i]);
  8. if(($arr[$i]%2)==1){
  9. array_push($arr7,$arr[$i]);
  10. }
  11. echo "<br>";
  12. }
  13. print_r($arr7);
  14. }
  15. arr($arr,$arr7);
  16. ?>

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

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