Correcting teacher:灭绝师太
Correction status:qualified
Teacher's comments:
<form action="" method='post'>
<input type="text" name='bef' value="<?php echo $_POST['bef']; ?>">
<select name='opt'>
<option value ="+" selected='<?php echo $_POST['opt']=="+" ? "selected": "" ?>' >+</option>
<option value ="-" selected='<?php echo $_POST['opt']=="-" ? "selected": "" ?>' >-</option>
<option value="*" selected='<?php echo $_POST['opt']=="*" ? "selected": "" ?>' >*</option>
<option value="/" selected='<?php echo $_POST['opt']=="/" ? "selected": "" ?>' >/</option>
</select>
<input type="text" name='aft' value="<?php echo $_POST['aft']; ?>">
<input type='submit' name='calc'></input>
</form>
<?php
// var_dump( $_POST['bef'],$_POST['aft'],$_POST['opt']);
$res;
if($_POST['bef'] =='' || $_POST['aft']=='')
{
echo '请填写完整得表达式!';
return;
};
switch ($_POST['opt']) {
case '+':
$res = $_POST['bef'] + $_POST['aft'];
break;
case '-':
$res = $_POST['bef'] - $_POST['aft'];
break;
case '*':
$res = $_POST['bef'] * $_POST['aft'];
break;
case '/':
$res = $_POST['bef'] / $_POST['aft'];
break;
}
echo '结果为:' . $res ;
?>
总结:第一次体验到php模板语法的魅力,嵌入到 HTML里面有点react的味道,但是相对于vue的模板语法和react的JSX确实比较难写,不过能体会到php的强大之处就是能嵌入到HTML,也是不错的