2014.01.07php简单计算器制作
1 <?php 2 header('content-type:text/html;charset=utf-8'); 3 $num1=true; //判断是否为真 4 $num2=true; //判断是否为真 5 $message=""; //设置为空 6 if(isset($_GET['submit'])){ 7 if($_GET['num1']==''){ 8 $num1=false; 9 $message.='第一个数字不能为空'; 10 } 11 if($_GET['num2']==''){ 12 $num2=false; 13 $message.='第二个数字不能为空'; 14 } 15 if($num1 && $num2){ 16 $sum=0; 17 switch($_GET['ysf']){ 18 case '+': 19 $sum=$_GET['num1']+$_GET['num2']; 20 break; 21 case '-': 22 $sum=$_GET['num1']-$_GET['num2']; 23 break; 24 case 'x': 25 $sum=$_GET['num1']*$_GET['num2']; 26 break; 27 case '/': 28 $sum=$_GET['num1']/$_GET['num2']; 29 break; 30 } 31 } 32 } 33 34 /*计算机制作*/ 35 ?> 36 <table align="center" border="1" width="500"> 37 <caption><h1>计算机</h1></caption> 38 <tr> 39 <form action="jsq.php"> 40 <td> 41 <input type='text' name='num1' value=''/> 42 </td> 43 <td> 44 <select name="ysf"> 45 <option>+</option> 46 <option>-</option> 47 <option>x</option> 48 <option>/</option> 49 50 </select> 51 </td> 52 <td> 53 <input type='text' name='num2' value=''/> 54 </td> 55 <td> 56 <input type='submit' name='submit' value='计算'/> 57 </td> 58 </form> 59 </tr> 60 <?php 61 if(isset($_GET['submit'])) 62 { 63 64 if($num1 && $num2){ 65 echo"<tr><td colspan='5'>"; 66 echo"计算结果为".$sum; 67 echo"</td></tr>"; 68 } 69 else{ 70 echo"<tr><td colspan='5'>"; 71 echo $message; 72 echo"</td></tr>"; 73 } 74 } 75 76 77 ?> 78 </table>
注意:if(isset($_GET['submit']))书写
if you don't try,you will never know!