Home  >  Article  >  php教程  >  A calculator based on a small program written in PHP basic language

A calculator based on a small program written in PHP basic language

WBOY
WBOYOriginal
2016-12-05 13:26:271484browse

Calculator based on a small program written in PHP basic language

Requirement: Enter numbers in the input box to perform addition, subtraction, multiplication and division operations (html+php)

Thought:

1 First, create an input box for inputting numbers and operators. Use the text attribute of input for numbers and the option attribute of sellct for operators

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

3 = This input box can be done using the submit of input. Just click the submit form and the content in the form will be sent to php

4 Determine the operators obtained from html and perform corresponding operations

5 After the operation is completed, the result must be returned to the form (that is, assigning a value to the form)

Code

Html code

//Method represents the form submission method. In this case, post submission is selected. Action is the accepted page. If it is empty, it means submitting to the current page.

                                          

                    

                                                 

                                           

        

PHP code

When the user clicks the submit button, the value will be passed through post. Now we need to accept the value in the form.

Make a few judgments before clicking

if (isset($_POST['submit'])) {

//isset detects whether the variable is set, exists, or non-NULL. The return value is Boolean. If the variable exists, it returns true, otherwise it is false; combined with $_POST["submit"], $_POST //Receive method= through the form 'post' method passing value

$num1 = $_POST['num1'];//Get the value in the first input box through the name attribute in the input

                       $select = $_POST['select'];//Same as above

                       $num2 = $_POST['num2'];//Same as above

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

            //

is_numeric() //

Detect whether the variable is a number or a numeric string

Return value, true, false Such as 100, ‘100’

(SWITCH ($ Select) {// $ Select is the operator from the previous

                                case '+':// According to the syntax of switch, if the value in case is equal to the value in the brackets of switch, then execute the sentence after case, if not, continue looking down                                                                                                                                                                                                                                                                                                                                                                                                                                case '-':

                                                                                                                 

                                                                                                                                                                                                                                                                       

                               case '*':

                                                                                                                                               $result = $num1*$num2;

                                                                                                                                                                                                                                                                      

                                                                                                                                                                                                            default:

U If ($ num2 == 0) {// Add a judgment, the division cannot be 0

& Echo "& lt; script & gt; alert ('The division of the input is 0, please re -enter') & lt;/script & gt;"; } Else {

                                                                                                   

                                                                                                                                                                                                     

                                                                                            

             }

                  }else{

当 // Echo When the user enters not the number, it may be a string to give the user a prompt

                                                                           echo "<script>alert('The input is not a number')</script>";

$ Num1 = $ num2 = $ result = "" ""; // Clear the content of the form in the form

             }

}

Screenshot of running results

Screenshot when entering the correct number

Click = after the number

Indicates that the value is not passed to the form in html,

Now let’s set the value of the form

//Set the value of value to the num value after operation in php

                                                                                                                        ~ ~ ~

                                                                                                                                                                                                                             

                          

                                               

                   

        

Run results

The input box now has content when the user does not click the submit button, so the value in the input box should be set to empty when the user does not click the submit button

Improve the coding, add an else{

at the end of the php code

$num1 =$num2 = $result = "";

}

Screenshot

When clicking on other operations, the operator in the middle is always +, screenshot

Code improvements

in html

                                                                                                                       

                              

                            

                                   

                                    

                                                                                                       

                          

                                               

             

      

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn