Blogger Information
Blog 11
fans 0
comment 1
visits 15474
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP 0805作业:简单的计算器和筛选数组中的偶数
州爱殇
Original
634 people have browsed it

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

  1. $arr = [23,3,45,6,78,8,34];
  2. $a=123;
  3. function filter ($arr){
  4. $newarr=[];
  5. if (is_array($arr)){
  6. foreach ($arr as $value){
  7. if ( $value % 2 == 0){
  8. $newarr[] += $value;
  9. }
  10. };
  11. return json_encode($newarr);
  12. }else{
  13. return "请传入数组数组,否则无法筛选";
  14. }
  15. }
  16. echo "传入非数组参数的结果::".filter($a);
  17. echo "<br>";
  18. echo "传入数组参数的结果".'<br>';
  19. $res=json_decode(filter($arr));
  20. print_r($res);

2.php实现简单的计算器功能(封装方法)

  1. function calcu($num1='0',$num2='0',$operate=''){
  2. if (!is_numeric($num1)){
  3. echo '请输入第一个正确的数字';
  4. }
  5. elseif (!is_numeric($num2) || $num2 == 0){
  6. echo '请输入第二个正确的数字';
  7. }
  8. elseif( empty($operate) ){
  9. echo '请输入正确的算数运算符';
  10. }
  11. else{
  12. switch ($operate){
  13. case '+':
  14. return '$num1+$num2='.($num1+$num2);
  15. break;
  16. case '-':
  17. return '$num1-$num2='.($num1-$num2);
  18. break;
  19. case '*':
  20. return '$num1*$num2='.($num1*$num2);
  21. break;
  22. case '/':
  23. return '$num1/$num2='.($num1/$num2);
  24. break;
  25. case '%':
  26. return '$num1%$num2='.($num1%$num2);
  27. break;
  28. }
  29. }
  30. }
  31. echo calcu('5',8,'+');
  32. echo calcu('0',8,'%');
  33. echo calcu('5',0,'*');

计算器功能,还没有实现当加减时第二个参数可以为0, 除和求余时第二个参数不能为0

Correcting teacher:灭绝师太灭绝师太

Correction status:qualified

Teacher's comments:作业完成了,但是两个作业都不够精简。第二个计算器不用封装函数,计算器你未实现的功能请参加我发到群里的0806预习资料1-cal.php脚本
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