PHP 简单计算器代码实现
生活随笔
收集整理的這篇文章主要介紹了
PHP 简单计算器代码实现
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
/**?簡單計算器*/error_reporting(E_ALL?&?~E_NOTICE);if(isset($_POST['sub'])){$a?=?$_POST['num1'];$b?=?$_POST['num2'];$var?=?$_POST['operator'];$flag?=?true;$errormess?=?"存在問題如下:<br/>";if($a?==?""){$flag?=?false;$errormess?.=?"第一個數不能為空<br/>";}elseif(!is_numeric($a)){$flag?=?false;$errormess?.=?"第一個數字必須是數字<br/>";}if($b?==?""){$flag?=?false;$errormess?.=?"第二個數不能為空<br/>";}elseif(!is_numeric($b)){$flag?=?false;$errormess?.=?"第二個數字必須是數字<br/>";}if($flag){$sum='';switch($var){case?'+':$sum=$a+$b;break;case?'-':$sum=$a-$b;break;case?'*':$sum=$a*$b;break;case?'/':$sum=$a/$b;break;?case?'%':$sum=$a%$b;break;}}
}?><html><head><title>簡單計算器</title></head><body><div><form?action="index.php"?method="post"><input?type="text"?name="num1"?value="<?php??echo?$_POST['num1']?>"?/>
<select?name="operator">
<option?<?php?echo?$_POST['operator']=='+'???"selected"?:?""??>??value="+"?>+</option>
<option?<?php?echo?$_POST['operator']=='-'???"selected"?:?""?;??>??value="-"?>-</option>
<option?<?php?echo?$_POST['operator']=='*'???"selected"?:?""?;??>??value="*"?>*</option>
<option?<?php?echo?$_POST['operator']=='/'???"selected"?:?""?;??>??value="/"?>/</option>
<option?<?php?echo?$_POST['operator']=='%'???"selected"?:?""?;??>??value="%"?>%</option>
</select>?<input?type="text"?name="num2"?value="<?php??echo?$_POST['num2']?>"?/><input?type="submit"?name="sub"?value="submit"></form></div>
<h4>計算結果</h4>
<?php?
if($flag){
echo?$_POST['num1'].$_POST['operator'].$_POST['num2']."=".$sum;
}else{echo?$errormess;
}
?></body>
</html>
轉載于:https://blog.51cto.com/11410485/1840123
總結
以上是生活随笔為你收集整理的PHP 简单计算器代码实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 让你全面了解清楚路由器的各种组网 让你全
- 下一篇: 2016最热门的PHP框架