Php learning-simple calculator implementation code

Source: Internet
Author: User

Copy codeThe Code is as follows: <Head>
<Title> PHP implements a simple calculator </title>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
</Head>
<? Php
// Single branch
If (isset ($ _ GET ["sub"])
{
$ Num1 = true; // whether the number 1 is null
$ Num2 = true; // whether the number 2 is null
$ Numa = true; // whether number 1 is a number
$ Numb = true; // whether the number 2 is a digit
$ Message = "";
// Judge whether the number 1 is null
If ($ _ GET ["num1"] = "")
{
$ Num1 = false;
$ Message. = "the first number cannot be blank ";
}
// Determine whether number 1 is a number
If (! Is_numeric ($ _ GET ["num1"])
{
$ Numa = false;
$ Message. = "the first number is not a number ";
}
// Determine whether number 2 is a number
If (! Is_numeric ($ _ GET ["num2"])
{
$ Numa = false;
$ Message. = "the second number is not a number ";
}
// Judge whether number 2 is null
If ($ _ GET ["num2"] = "")
{
$ Num2 = false;
$ Message. = "the second number cannot be blank ";
}
If ($ num1 & $ num2 & $ numa & $ numb)
{
$ Sum = 0;
// Multiple branches
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 = "500">
<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 = "computing" name = "sub">
</Td>
</Tr>
<? Php
If (isset ($ _ GET ["sub"])
{
Echo '<tr> <td colspan = "4"> ';
If ($ num1 & $ num2 & $ numa & $ numb)
{
Echo "Result :". $ _ GET ["num1"]. "". $ _ GET ["ysf"]. "". $ _ GET ["num2"]. "= ". $ sum;
}
Else
{
Echo $ message;
}
Echo '</td> </tr> ';
}
?>
</Form>
</Table>
</Body>
</Html>

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.