• <blockquote id="faig0"><i id="faig0"></i></blockquote>
    \r\n\t\t第一個操作數(shù)不能為空<\/font>\";\r\n\t\t\t\t\tunset($_POST[\"sub\"]);\t\/\/取消表單中的提交變量,后面計算判斷時將不執(zhí)行\(zhòng)r\n\t\t\t\t}\r\n\t\t\t\tif(empty($_POST[\"num2\"])){\t\/\/如果第二個第一個操作數(shù)為空輸出錯誤信息,并停止計算\r\n\t\t\t\t\techo \"第二個操作數(shù)不能為空<\/font>\";\r\n\t\t\t\t\tunset($_POST[\"sub\"]);\t\/\/取消表單中的提交變量,后面計算判斷時將不執(zhí)行\(zhòng)r\n\t\t\t\t}\r\n\t\t\t\t$oper=$_POST[\"oper\"];\t\/\/獲取操作符號\r\n\t\t\t\t$num1=$_POST[\"num1\"];\t\/\/獲取第一個操作數(shù)\r\n\t\t\t\t$num2=$_POST[\"num2\"];\t\/\/獲取第二個操作數(shù)\r\n\t\t\t\tif($oper == \"\/\"){\r\n\t\t\t\t\tif($num2 == 0){\r\n\t\t\t\t\t\techo \"0不能作為除數(shù)使用<\/font>\";\r\n\t\t\t\t\t\tunset($_POST[\"sub\"]);\r\n\t\t\t\t\t}\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t\t\r\n\t\t?>\r\n\t\t\r\n\t\t\r\n\t\t\t \r\n\t\t\t\t

    簡單的計算器<\/h2><\/caption>\r\n\t\t\t\t\r\n\t\t\t\t\t\"><\/td>\r\n\t\t\t\t\t\r\n\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t>+<\/option>\r\n\t\t\t\t\t\t\t>-<\/option>\r\n\t\t\t\t\t\t\t>*<\/option>\r\n\t\t\t\t\t\t\t>\/<\/option>\r\n\t\t\t\t\t\t<\/select>\r\n\t\t\t\t\t<\/td>\r\n\t\t\t\t\t\"><\/td>\r\n\t\t\t\t\t<\/td>\r\n\t\t\t\t<\/tr>\r\n\t\t\t\t\";\r\n\t\t\t\t\t\techo \"計算結果:$num1 $oper $num2 = $sum\";\r\n\t\t\t\t\t\techo \"<\/td><\/tr>\";\r\n\t\t\t\t\t}\r\n\t\t\t\t?>\r\n\t\t\t<\/form>\r\n\t\t<\/table>\r\n\t<\/body>\r\n<\/html><\/pre>"}

    亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

    Home php教程 PHP源碼 一個簡單的在線計算器

    一個簡單的在線計算器

    May 25, 2016 pm 05:08 PM

    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=&#39;red&#39;>第一個操作數(shù)不能為空</font>";
    					unset($_POST["sub"]);	//取消表單中的提交變量,后面計算判斷時將不執(zhí)行
    				}
    				if(empty($_POST["num2"])){	//如果第二個第一個操作數(shù)為空輸出錯誤信息,并停止計算
    					echo "<font color=&#39;red&#39;>第二個操作數(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=&#39;red&#39;>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í)行以及是否輸出計算結果**/
    					if(isset($_POST["sub"]) && !empty($_POST["sub"])){
    						$sum = 0; //聲明一個存放計算結果的變量,初始值為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;
    						}
    						//以下的三行是在表格的新行中輸出計算結果
    						echo "<tr><td colspan=&#39;4&#39; align=&#39;center&#39;>";
    						echo "計算結果:$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 AI Tool

    Undress images for free

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Clothoff.io

    Clothoff.io

    AI clothes remover

    Video Face Swap

    Video Face Swap

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

    Hot Tools

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    Zend Studio 13.0.1

    Zend Studio 13.0.1

    Powerful PHP integrated development environment

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)