PHP的基礎計算機

來源:互聯網
上載者:User

標籤:style   blog   http   color   io   os   ar   for   sp   

設計一個計算的功能,該功能能夠完成運算並且能夠對不合理的資料進行驗證並且給出錯誤提示.

規則: 第一個數,第二個數不能夠為空白

如果操作符是/,第二個數數不能夠為0.

 1 <?php 2 header(‘Content-Type:text/html; charset=utf-8‘); 3 /*設計一個計算的功能,該功能能夠完成運算並且能夠對不合理的資料進行驗證並且給出錯誤提示. 4 規則: 第一個數,第二個數不能夠為空白 5 如果操作符是/,第二個數數不能夠為0.*/ 6  7 function jsq($num1,$oper,$num2){ 8     //檢測資料不可為空並提示 9     if(!isset($num1) || !is_numeric($num1)){10         $error = <<<ERROR11             <script>12                 alert(‘第一個數不合法‘);13             </script>14 ERROR;15         return $error;16     }17     if(!isset($num2) || !is_numeric($num2)){18         $error = <<<ERROR19             <script>20                 alert(‘第二個數不合法‘);21             </script>22 ERROR;23         return $error;24     }25 26     if($oper == "+"){27         $result = $num1 + $num2;28     }elseif($oper == "-"){29         $result = $num1 - $num2;30     }elseif($oper == "*"){31         $result = $num1 * $num2;32     }elseif($oper = "/"){33         if($num2 == 0){34             $error = <<<ERROR35                 <script>36                     alert(‘第二個數不能為0‘);37                 </script>38 ERROR;39         return $error;40         }41         $result = $num1 / $num2;42     }43     return $result;44 }45 46 if($_SERVER[‘REQUEST_METHOD‘] == "POST"){47     $res = jsq($_POST[‘num1‘],$_POST[‘oper‘],$_POST[‘num2‘]);48 }49 ?>50 51 <h2>用php寫的基礎計算機</h2>52 <form action="<?php echo $_SERVER[‘PHP_SELF‘]; ?>" method="post">53     第一個數:<input type="text" name="num1" /><p>54     操作符:<select name="oper">55                 <option value="+"> + </option>56                 <option value="-"> - </option>57                 <option value="*"> * </option>58                 <option value="/"> / </option>59            </select><p>60     第二個數:<input type="text" name="num2" /><p>61     <input type="submit" value="計算" />62 63 </form>64 計算結果為:<?php echo isset($res)?$res:""; ?>
View Code

 

PHP的基礎計算機

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.