Blogger Information
Blog 23
fans 0
comment 0
visits 18988
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
0805 一.将数组的偶数去出, 二.尝试实现计算器功能
手机用户1617360551
Original
469 people have browsed it

一.将数组的偶数去出, 二.尝试实现计算器功能

一.将一个数组中的偶数取出

方法一 用过滤器的方法

  1. <?php
  2. $arr = [3,5,6,9,15,18,22];
  3. function demo($sum)
  4. {
  5. return !($sum % 2); //声明一个函数,返回取余等于零的为ture
  6. }
  7. $sum1= array_filter($arr,'demo');//将数组中返回为ture的保存到一个新数组中
  8. var_dump($sum1);

方法二 用数组遍历的方法

  1. <?php
  2. $arr = [12,15,18,21,26,30,33,46];
  3. function arr1($number)
  4. {
  5. $arrDouble=[];
  6. foreach($number as $arr2)
  7. {
  8. //将数组中的成员,取余为0为FLASE,然后去反为TRUE,返回为TURE的成员通过入列的方式传到一个新数组中
  9. if (!($arr2 % 2)) array_push($arrDouble,$arr2);
  10. }
  11. return $arrDouble;
  12. //函数返回一个装有偶数的新数组
  13. }
  14. print_r(arr1($arr));

二.尝试实现一个计算器功能

  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. <?php
  10. $error="";
  11. $num1 = $_POST["num1"] ?? "";
  12. $num2 = $_POST["num2"] ?? "";
  13. $operator = $_POST["operator"] ?? "";
  14. if(isset( $_POST["sub"])){
  15. if($num1 == ""){
  16. $error .= "请输入第一个数据";
  17. }elseif(!is_numeric($num1)){
  18. $error .= "必须是数字";
  19. }
  20. if($num2 == ""){
  21. $error .= "请输入第二个数据";
  22. }elseif(!is_numeric($num2)){
  23. $error .= "必须为数字";
  24. }
  25. if(($operator == '/' || $operator == '%') && $num2 == 0)
  26. {
  27. $error .= "被除数不能为0";
  28. }
  29. }
  30. ?>
  31. <body>
  32. <table align="center" border="1" width="600" >
  33. <caption><h2>计算器</h2></caption>
  34. <form action=""method="post">
  35. <tr>
  36. <td>
  37. <input type="text" name="num1" value="<?php echo $num1 ?>">
  38. </td>
  39. <td>
  40. <select name="operator">
  41. <option value="+" <?php if($operator == "+") echo "selected"?>>+</option>
  42. <option value="-" <?php if($operator == "-") echo "selected"?>>-</option>
  43. <option value="*" <?php if($operator == "*") echo "selected"?>>*</option>
  44. <option value="/" <?php if($operator == "/") echo "selected"?>>/</option>
  45. <option value="%" <?php if($operator == "%") echo "selected"?>>%</option>
  46. </select>
  47. </td>
  48. <td>
  49. <input type="text" name = "num2" value="<?php echo $num2 ?>" >
  50. </td>
  51. <td>
  52. <input type="submit" name="sub" value="计算">
  53. </td>
  54. </tr>
  55. <?php
  56. if(isset($_POST["sub"])){
  57. echo '<tr><td colspan = "5" align ="center">';
  58. if(empty($error)){
  59. $sum = 0;
  60. switch($operator){
  61. case "+":
  62. $sum = $num1 + $num2 ;
  63. break;
  64. case "-":
  65. $sum = $num1 - $num2 ;
  66. break;
  67. case "*":
  68. $sum = $num1 * $num2 ;
  69. break;
  70. case "/":
  71. $sum = $num1 / $num2;
  72. break;
  73. case "%":
  74. $sum = $num1 % $num2;
  75. break;
  76. }
  77. echo "结果: {$num1} {$operator} {$num2} = {$sum} ";
  78. }else{ echo $error;}
  79. echo '</td></tr>';
  80. }
  81. ?>
  82. </form>
  83. </table>
  84. </body>
  85. </html>
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