php代碼
<html> <head> <meta http-equiv="content-type" content="text/html; charset=utf8"> <title>一個簡單的計算器</title> </head> <body> <?php //如果用戶單擊了計算按鈕才存在表單變量,才能接收表單數(shù)據(jù)和對數(shù)據(jù)進行驗證 //以下if語句判斷是否將表單中的兩個操作數(shù)提交到本頁面,如果沒有則不執(zhí)行 if(isset($_POST["num1"]) && isset($_POST["num2"])){ if(empty($_POST["num1"])){ //如果第一個第一個操作數(shù)為空輸出錯誤信息,并停止計算 echo "<font color='red'>第一個操作數(shù)不能為空</font>"; unset($_POST["sub"]); //取消表單中的提交變量,后面計算判斷時將不執(zhí)行 } if(empty($_POST["num2"])){ //如果第二個第一個操作數(shù)為空輸出錯誤信息,并停止計算 echo "<font color='red'>第二個操作數(shù)不能為空</font>"; unset($_POST["sub"]); //取消表單中的提交變量,后面計算判斷時將不執(zhí)行 } $oper=$_POST["oper"]; //獲取操作符號 $num1=$_POST["num1"]; //獲取第一個操作數(shù) $num2=$_POST["num2"]; //獲取第二個操作數(shù) if($oper == "/"){ if($num2 == 0){ echo "<font color='red'>0不能作為除數(shù)使用</font>"; unset($_POST["sub"]); } } } ?> <!--以HTML表格的形式輸出計算器的用戶操作界面--> <table border="1" align="center" width="400"> <form action="" method="post"> <!--使用POST方法提交給本頁面--> <caption><h2>簡單的計算器</h2></caption> <tr> <td><input type="text" size="10" name="num1" value="<?php if(!empty($num1)){echo $num1;}?>"></td> <td> <select name="oper"> <option value="+" <?php if($oper == "+"){echo "selected";}?>>+</option> <option value="-" <?php if($oper == "-"){echo "selected";}?>>-</option> <option value="*" <?php if($oper == "*"){echo "selected";}?>>*</option> <option value="/" <?php if($oper == "/"){echo "selected";}?>>/</option> </select> </td> <td><input type="text" size="10" name="num2" value="<?php if(!empty($num2)){echo $num2;}?>"></td> <td><input type="submit" name="sub" value="計算"></td> </tr> <?php /**通過表單傳過來的$_POST["sub],判斷是否單擊了計算按鈕 來決定以下計算是否執(zhí)行以及是否輸出計算結(jié)果**/ if(isset($_POST["sub"]) && !empty($_POST["sub"])){ $sum = 0; //聲明一個存放計算結(jié)果的變量,初始值為0 echo $sum; //使用switch語句,通過表單選擇的操作符來決定執(zhí)行哪種運算 switch($oper){ case "+": $sum = $num1+$num2; break; case "-": $sum = $num1-$num2; break; case "*": $sum = $num1*$num2; break; case "/": $sum = $num1/$num2; break; } //以下的三行是在表格的新行中輸出計算結(jié)果 echo "<tr><td colspan='4' align='center'>"; echo "計算結(jié)果:$num1 $oper $num2 = $sum"; echo "</td></tr>"; } ?> </form> </table> </body> </html>
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article
Guide: Stellar Blade Save File Location/Save File Lost/Not Saving
4 weeks ago
By DDD
Oguri Cap Build Guide | A Pretty Derby Musume
2 weeks ago
By Jack chen
Agnes Tachyon Build Guide | A Pretty Derby Musume
2 weeks ago
By Jack chen
Dune: Awakening - Advanced Planetologist Quest Walkthrough
4 weeks ago
By Jack chen
Date Everything: Dirk And Harper Relationship Guide
4 weeks ago
By Jack chen

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)
