Correcting teacher:PHPz
Correction status:qualified
Teacher's comments:
<?php
print_r($_REQUEST);
if(!empty($_POST)){
if($_POST['opt'] == '+')
{
$num = (int)$_POST['x'] + (int)$_POST['y'];
}else if($_POST['opt'] == '-')
{
$num = (int)$_POST['x'] - (int)$_POST['y'];
}else if($_POST['opt'] == '*')
{
$num = (int)$_POST['x'] * (int)$_POST['y'];
}else if($_POST['opt'] == '/')
{
$num = (int)$_POST['x'] / (int)$_POST['y'];
}else if($_POST['opt'] == '%')
{
$num = (int)$_POST['x'] % (int)$_POST['y'];
}
echo 'x'. $_POST['opt'] . 'y 计算结果:' .$num;
}else if($_POST['opt'] == '%')
{
$num = (int)$_POST['x'] % (int)$_POST['y'];
}
?>
<!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>Document</title>
</head>
<body>
<h1>计算器</h1>
<form action="" method="post">
<input type="number" name="x" value="<?=isset($_POST['x'])?$_POST['x']:''; ?>" />
<select name="opt">
<option value="+" <?=isset($_POST['opt']) && $_POST['opt']=='+'?'selected':'' ?>>+</option>
<option value="-" <?=isset($_POST['opt']) && $_POST['opt']=='-'?'selected':'' ?>>-</option>
<option value="*" <?=isset($_POST['opt']) && $_POST['opt']=='*'?'selected':'' ?>>*</option>
<option value="/" <?=isset($_POST['opt']) && $_POST['opt']=='/'?'selected':'' ?>>/</option>
<option value="%" <?=isset($_POST['opt']) && $_POST['opt']=='%'?'selected':'' ?>>%</option>
</select>
<input type="number" name="y" value="<?=isset($_POST['y'])?$_POST['y']:''; ?>" />
<input type="submit" value="计算" />
</form>
</body>
</html>
<?php
function get_url($url,$data,$is_post=0){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL ,'http://apis.juhe.cn/simpleWeather/query?key=abc4b64ae7656b460723402175a5650b&city=合肥');
if($is_post == 0){
if(!empty($data)){
$url .= '?';
foreach($data as $k=>$v){
$url .= $k . '=' . $v . '&';
}
}
}
curl_setopt($ch, CURLOPT_URL ,$url);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, 'Data');
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
if($is_post == 1){
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS , $data);
}
$html = curl_exec($ch);
if(curl_errno($ch)){
return curl_errno($ch);
}
curl_close($ch);
return $html;
}
$data = [
'key' => 'abc4b64ae7656b460723402175a5650b',
'city' => '合肥'
];
echo get_url('http://apis.juhe.cn/simpleWeather/query',$data);
?>