Correcting teacher:天蓬老师
Correction status:qualified
Teacher's comments:
<?php
$arr = [23,3,45,6,78,8,34];
function odd(array $arr):array
{
$newArr=[];
for($i=0;$i<count($arr);$i++){
if($arr[$i]%2==0){
$newArr[]=$arr[$i];
//或者使用array_push($newArr,$arr[$i]);
}
}
return $newArr;
}
var_dump(odd($arr));
?>
运行效果如下
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>计算器</title>
</head>
<body>
<?php
error_reporting(E_ALL &~E_NOTICE);
if(isset($_POST['sub'])):
if($_POST['opt']=='/'
&&$_POST['num2']==0) $mess="
<span style='color:red;'>除数不能为0</span>";
endif;
?>
<h1>计算器</h1>
<table>
<form action="" method="POST">
<tr>
<td>
<input type="number" name="num1" required value="<?=$_POST['num1']?>">
</td>
<td>
<select name="opt">
<option value="+" <?=$_POST['opt']=="+"?"selected":"" ?>>+</option>
<option value="-" <?=$_POST['opt']=="-"?"selected":"" ?>>-</option>
<option value="*" <?=$_POST['opt']=="*"?"selected":"" ?>>*</option>
<option value="/" <?=$_POST['opt']=="/"?"selected":"" ?>>/</option>
</select>
</td>
<td>
<input type="number" name="num2" required value="<?=$_POST['num2']?>">
</td>
<td>
<input type="submit" name="sub" value="计算">
<?php
if(!$mess && isset($_POST['sub']) ):
switch($_POST['opt']):
case "+":
$sum=(int)$_POST['num1']+(int)$_POST['num2'];break;
case "-":
$sum=(int)$_POST['num1']-(int)$_POST['num2'];break;
case "*":
$sum=(int)$_POST['num1']*(int)$_POST['num2'];break;
case "/":
$sum=(int)$_POST['num1']/(int)$_POST['num2'];break;
endswitch;
$res="计算结果:{$_POST['num1']}
{$_POST['opt']}{$_POST['num2']}={$sum}";
echo "<span style='color: green;'>{$res}</span>";
else:
echo $mess;
endif;
?>
</td>
</tr>
</form>
</table>
</body>
</html>
运行效果如下: