<!-- 作者:郑伟钊 时间:2017-01-18 描述:这是一个简单的计算器 --> <html > <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>简单的计算器</title> </head> <body> <form method="post" action=""> 数1:<input type="text", name="n1" />+ 数2:<input type="text", name="n2" />= <input type="submit" name="sub1" value="计算加法" /> <p> <?php //这段是加法代码段 if(isset($_POST['sub1']))//这是一个点击事件,当点击了sub1时候,执行下面的代码 { $sub1=$_POST['n1']+$_POST['n2'];//获取将前面的两个框的值,相加赋值给$sub1; echo $_POST['n1'].'+'.$_POST['n2'].'='.$sub1;//输出结果 } ?> <p> <form method="post" action=""> 数1:<input type="text" name="n3" />- 数2:<input type="text" name="n4" />= <input type="submit" name="sub2" value="计算减法" /> <p> <?php //这段是减法代码段 if(isset($_POST['sub2'])) //点击事件 { $sub2=$_POST['n3']-$_POST['n4'];//获取相应的值 echo $_POST['n3'].'-'.$_POST['n4'].'='.$sub2; } ?> <p> <form method="post" action=""> 数1:<input type="text" name="n5" />* 数2:<input type="text" name="n6" />= <input type="submit" name="sub3" value="计算乘法" /> <p> <?php //这是乘法的代码段 if(isset($_POST['sub3'])){ $sub3=$_POST['n5']*$_POST['n6']; echo $_POST['n5'].'*'.$_POST['n6'].'='.$sub3; } ?> <p> <form method="post" action=""> 数1:<input type="text" name="n7" />/ 数2:<input type="text" name="n8" />= <input type="submit" name="sub4" value="计算除法" /> <p> <?php //这是除法代码段 if(isset($_POST['sub4'] )){ $sub4=$_POST['n7']/$_POST['n8']; echo $_POST['n7'].'/'.$_POST['n8'].'='.$sub4; } ?>