A calculator for small programs written in the basic php language.

Source: Internet
Author: User

A calculator for small programs written in the basic php language.

Calculator for small programs written in basic php language

Requirement: enter a number in the input box for addition, subtraction, multiplication, and Division operations (html + php)

Ideas:

1 first, you must create an input box for the input numbers and operators. The numbers use the text attribute of the input, and the operators use the option attribute of selelct.

2. Click the = sign in the input box to perform the corresponding operation,

The input box "3 =" can be used as the input submit. You only need to click the content in the submit form to pass it to php.

4. Determine the operators obtained from html for corresponding operations

5. After the computation is completed, you must return the result to the form (that is, assign a value to the form value)

 

Code

Html code

<Form method = "post" action = ""> // method indicates the form submission method. In this case, select the page on which post submits an action, if it is null, it is submitted to the current page.

<Input type = "text" name = "num1">

<Select name = "select">

<Option value = "+" >+ </option>

<Option value = "-">-</option>

<Option value = "*"> * </option>

<Option value = "/">/</option>

</Select>

<Input type = "text" name = "num2">

<Input type = "submit" name = "submit" value = "=">

<Input type = "text" name = "result">

</Form>

 

PHP code

When you click the submit button value, it will be passed through post. Now you need to accept the value in the form.

Make several judgments before clicking

If (isset ($ _ POST ['submit ']) {

// Isset checks whether the variable is set, exists, or not NULL. the return value is boolean. If the variable exists, true is returned; otherwise, false is returned. Combined with $ _ POST ["submit"], $ _ POST // receive the value transmitted by the form method = 'post' method

$ Num1 = $ _ POST ['num1']; // obtain the value in the first input box, which is obtained through the name attribute in input.

$ Select = $ _ POST ['select']; // same as above

$ Num2 = $ _ POST ['num2']; // same as above

If (is_numeric ($ num1) & is_numeric ($ num2 )){

//Is_numeric ()//Checks whether the variable is a numeric or numeric string.Return Value, true, false, for example, 100, '123'

 

Switch ($ select) {// $ select is the previous Operator

Case '+': // according to the switch syntax, if the value in case is equal to the value in switch brackets, execute the sentence after case. If the value does not match, continue searching.

$ Result = $ num1 + $ num2;

Break;

Case '-':

$ Result = $ num1-$ num2;

Break;

Case '*':

$ Result = $ num1 * $ num2;

Break;

Default:

If ($ num2 = 0) {// Add a judgment. The divisor cannot be 0.

Echo "<script> alert ('input division is 0, please input again ') </script> ";

} Else {

$ Result = $ num1/$ num2;

Break;

}

}

} Else {

// Echo: when the user does not input a number, it may be a string, the user is prompted

Echo "<script> alert ('input is not number') </script> ";

$ Num1 = $ num2 = $ result = ""; // clear the content in the form.

}

}

Running result

 

 

When you enter the correct number

 

Click =

 

The description value is not passed to the form in html,

Set the value of the form.

<Input type = "text" name = "num1" value = "<? Php echo $ num1?> "> // Set the value to the num value after calculation in php.

<Select name = "select">

<Option value = "+" >+ </option>

<Option value = "-">-</option>

<Option value = "*"> * </option>

<Option value = "/">/</option>

</Select>

<Input type = "text" name = "num2" value = "<? Php echo $ num2?> ">

<Input type = "submit" name = "submit" value = "=">

<Input type = "text" name = "result" value = "<? Php echo $ result?> ">

</Form>

Running result

 

When the user does not click the submit button, the input box contains content. Therefore, when the user does not click the submit button, set the value in the input box to null.

Improved with code, and added an else {

 

$ Num1 = $ num2 = $ result = "";

}

 

When you click another operation, the intermediate operator is always +,

 

Code Improvement

In html

<Select name = "select">

<Option value = "+" <? Php if ($ select = '+') echo 'selected'?> + </Option>

// Select has a property selected, which is selected by default when set. Therefore, you must compare the property with the value passed by php. true indicates that false indicates that the selected property is not selected.

<Option value = "-" <? Php if ($ select = '-') echo 'selected'?> -</Option>

<Option value = "*" <? Php if ($ select = '*') echo 'selected'?> * </Option>

<Option value = "/" <? Php if ($ select = '/') echo 'selected'?> /</Option>

</Select>

View results

 

When the user first comes in

 

Set the default value in selecte.

Code

$ Select = "+"

Basic functions have been completed

Total code

<! DOCTYPE html>

<Html lang = "en">

<Head>

<Meta charset = "UTF-8">

<Title> Document </title>

</Head>

<Body>

<? Php

If (isset ($ _ POST ['submit ']) {

// Isset checks whether the variable is set, exists, or not NULL. the return value is boolean. If the variable exists, true is returned; otherwise, false is returned. Combined with $ _ POST ["submit"], $ _ POST // receive the value transmitted by the form method = 'post' method

$ Num1 = $ _ POST ['num1']; // obtain the value in the first input box, which is obtained through the name attribute in input.

$ Select = $ _ POST ['select']; // same as above

$ Num2 = $ _ POST ['num2']; // same as above

If (is_numeric ($ num1) & is_numeric ($ num2 )){

// Is_numeric () // checks whether the variable is a numeric or numeric string return value. The optional values are true and false, for example, 100 and '123'

 

Switch ($ select) {// $ select is the previous Operator

Case '+': // according to the switch syntax, if the value in case is equal to the value in switch brackets, execute the sentence after case. If the value does not match, continue searching.

$ Result = $ num1 + $ num2;

Break;

Case '-':

$ Result = $ num1-$ num2;

Break;

Case '*':

$ Result = $ num1 * $ num2;

Break;

Default:

If ($ num2 = 0) {// Add a judgment. The divisor cannot be 0.

Echo "<script> alert ('input division is 0, please input again ') </script> ";

} Else {

$ Result = $ num1/$ num2;

Break;

}

}

} Else {

// Echo: when the user does not input a number, it may be a string, the user is prompted

Echo "<script> alert ('input is not number') </script> ";

$ Num1 = $ num2 = $ result = ""; // clear the content in the form.

}

} Else {

$ Num1 = $ num2 = $ result = "";

$ Select = "+ ";

}

?>

<Form method = "post" action = ""> <! -- // Method indicates the form submission method. In this case, the post submission action is an accepted page. If it is null, the page is submitted to the current page. -->

<Input type = "text" name = "num1" value = "<? Php echo $ num1?> ">

<Select name = "select">

<Option value = "+" <? Php if ($ select = '+') echo 'selected'?> + </Option>

<Option value = "-" <? Php if ($ select = '-') echo 'selected'?> -</Option>

<Option value = "*" <? Php if ($ select = '*') echo 'selected'?> * </Option>

<Option value = "/" <? Php if ($ select = '/') echo 'selected'?> /</Option>

</Select>

<Input type = "text" name = "num2" value = "<? Php echo $ num2?> ">

<Input type = "submit" name = "submit" value = "=">

<Input type = "text" name = "result" value = "<? Php echo $ result?> ">

</Form>

</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.