Calculator for small programs written in basic php language

Source: Internet
Author: User
Small program calculator based on php basic language calculator based on small program written in php basic 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

 

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 is not NULL. The return value is Boolean. if the variable exists, true is returned, otherwise, it is false. combine $ _ POST ["submit"], $ _ POST // receives the value of the form's method = 'post' method $ num1 = $ _ post ['num1']; // retrieves the value in the first input box, get $ select =$ _ POST ['select'] through the name attribute in input; // same as $ num2 =$ _ POST ['num2']; // same as if (is_numeric ($ num1) & is_numeric ($ num2) {// is_numeric () // checks whether the variable is returned by a number or number string, true, false, for example, 100, '200' switch ($ select) {// $ select is the preceding operator case '+': // according to the switch syntax, if the value in case is the same as the value in switch brackets, execute the sentence after case. if it is not the same, continue to find $ 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 ('Enter a division of 0, please input again ') script ";} else {$ result = $ num1/$ num2; break ;}} else {// echo when the user does not enter a number, if it is a string, the user is prompted with 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

Set the default value in selecte.

Code

$ Select = "+"

Basic functions have been completed

Total code

       
        Document       
 

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.