PHP Learning Simple Calculator Implementation Code _php Foundation

Source: Internet
Author: User
Copy Code code as follows:

<title>php Implement Simple Calculator </title>
<meta http-equiv= "Content-type" content= "text/html;charset=gb2312" >
<?php
One-Way Branch
if (Isset ($_get["sub"))
{
$num 1=true;//Number 1 is an empty tag
$num 2=true;//number 2 is an empty tag
$numa =true;//Number 1 is a number
$numb =true;//Number 2 digits
$message = "";
Determine if the number 1 is empty
if ($_get["NUM1"]== "")
{
$num 1=false;
$message. = "The first number cannot be empty";
}
Judge whether the number 1 is a number
if (!is_numeric ($_get["NUM1"]))
{
$numa =false;
$message. = "First number is not a number";
}
Judge whether the number 2 is a number
if (!is_numeric ($_get["num2"]))
{
$numa =false;
$message. = "Second number is not a number";
}
Determine if the number 2 is empty
if ($_get["num2"]== "")
{
$num 2=false;
$message. = "Second number cannot be null";
}
if ($num 1 && $num 2 && $numa && $numb)
{
$sum = 0;
Multi-Channel Branch
Switch ($_get["YSF"]) {
Case "+":
$sum =$_get["NUM1"]+$_get["num2"];
Break
Case "-":
$sum =$_get["NUM1"]-$_get["num2"];
Break
Case "X":
$sum =$_get["NUM1"]*$_get["num2"];
Break
Case "/":
$sum =$_get["NUM1"]/$_get["num2"];
Break
Case "%":
$sum =$_get["NUM1"]%$_get["num2"];
Break
}
}
}
?>
<body>
<table align= "Center" border= "1" width= ">"
<caption><form action= "jisuanqi.php" >
<tr>
<td>
<input type= "Text" size= "5" name= "NUM1" "value=" <?php echo $_get["NUM1"];?> ">
</td>
<td>
<select name= "YSF" >
<option value= "+" <?php if ($_get["YSF"]== "+") echo "selected";?>>+</option>
<option value= "-" <?php if ($_get["YSF"]== "-") echo "selected";?>>-</option>
<option value= "x" <?php if ($_get["YSF"]== "x") echo "selected";?>>x</option>
<option value= "/" <?php echo $_get[ysf "]=="/"?" Selected ":";?>>/</option>
<option value= "%" <?php if ($_get["YSF"]== "%") echo "selected";?>>%</option>
</select>
</td>
<td>
<input type= "Text" size= "5" name= "num2" "value=" <?php echo $_get["num2"];?> ">
</td>
<td>
<input type= "Submit" value= "Calculate" name= "sub" >
</td>
</tr>
<?php
if (Isset ($_get["sub"))
{
Echo ' <tr><td colspan= 4 ' > ';
if ($num 1 && $num 2 && $numa && $numb)
{
echo "The result is:". $_get["NUM1"]. " ". $_get[" YSF "]." ". $_get[" num2 "]." = ". $sum;
}
Else
{
Echo $message;
}
Echo ' </td></tr> ';
}
?>
</form>
</table>
</body>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.