Blogger Information
Blog 20
fans 0
comment 0
visits 10789
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
给定一个数组$arr = [23,3,45,6,78,8,34],筛选其偶数成员组成新的数组返回,请封装函数 + 简单计算器功能
麦兜的故事
Original
453 people have browsed it

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

  1. <?php
  2. // 给定一个数组
  3. $arr = [23,3,45,6,78,8,34];
  4. // 封装函数
  5. function grouping(array $arr): array {
  6. // 先声明一个新数组
  7. $newArr = [];
  8. for($i = 0; $i < count($arr); $i++) {
  9. if($arr[$i] % 2 == 0){
  10. $newArr[] = $arr[$i];
  11. }
  12. }
  13. return $newArr;
  14. }
  15. $return = grouping($arr);
  16. var_dump($return);
  17. ?>

返回的结果

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

  1. <?php
  2. error_reporting(E_ALL & ~E_NOTICE);
  3. if($_POST['button']):
  4. if($_POST["choice"] == "/" && $_POST["num2"] == 0 || $_POST["choice"] == "%" && $_POST["num2"] == 0):
  5. echo "除数不能为0";
  6. endif;
  7. endif;
  8. ?>
  9. <h2>加减乘除计算器</h2>
  10. <form action="" method="POST">
  11. <input type="number" name="num1" id="num1" required value="<?=$_POST['num1']?>">
  12. <select name="choice" id="choice">
  13. <option value="">--请选择--</option>
  14. <option value="+" <?=$_POST["choice"] == "+" ? "selected" : "" ?>>+</option>
  15. <option value="-" <?=$_POST["choice"] == "-" ? "selected" : "" ?>>-</option>
  16. <option value="*" <?=$_POST["choice"] == "*" ? "selected" : "" ?>>*</option>
  17. <option value="/" <?=$_POST["choice"] == "/" ? "selected" : "" ?>>/</option>
  18. <option value="%" <?=$_POST["choice"] == "%" ? "selected" : "" ?>>%</option>
  19. </select>
  20. <input type="number" name="num2" id="num2" required value="<?=$_POST['num2']?>">
  21. <input type="submit" value="等于" name="button">
  22. </form>
  23. <?php
  24. if(isset($_POST['button'])):
  25. switch($_POST['choice']){
  26. case "+":
  27. $sum = (int)$_POST['num1'] + (int)$_POST['num2'];break;
  28. case "-":
  29. $sum = (int)$_POST['num1'] - (int)$_POST['num2'];break;
  30. case "*":
  31. $sum = (int)$_POST['num1'] * (int)$_POST['num2'];break;
  32. case "/":
  33. $sum = (int)$_POST['num1'] / (int)$_POST['num2'];break;
  34. case "%":
  35. $sum = (int)$_POST['num1'] % (int)$_POST['num2'];break;
  36. }
  37. echo "<span style='color:green;'>{$sum}</span>";
  38. endif;
  39. ?>
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