Calculator for small programs written in PHP base language

Source: Internet
Author: User
Calculator for small programs written in PHP base language

Requirements: Enter numbers in the input box to add, subtract, multiply, divide (html+php)

Ideas:

1 first to create input boxes for entering numbers and operators, number with the Text property of input, operator with SELELCT option property

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

3 = number This input box can be done with the submit of input, just click on the contents of the submit form and pass it to PHP.

4 Judging the operators that are obtained from HTML for the corresponding operation

5 When the operation is complete, return the result to the table consignments (which is the value assignment to the form)

Code

HTML code

<form method= "POST" action= "" >//method represents the way the form was submitted, this case selected the Post submission action is the accepted page, and the null representation 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 = "Su Bmit "value=" = ">              <input type =" text "name=" result ">       </form>

PHP code

When the user clicks on the Submit button the value is passed through post and now accepts the value in the form.

Make a few judgments before you click

if (Isset ($_post[' submit '))) {//isset detects if the variable is set, exists, or is not NULL, the return value is Boolean, if the variable exists returns true, otherwise false; combined with $_post["submit"], $_post/ /Receive Method= ' Post ' method via form $num1 = $_post[' num1 '];//get the value in the first input box, get $select = $_post[' select '];/by the Name property in input /Ibid. $num 2 = $_post[' num2 '];//ditto if (Is_numeric ($num 1) && is_numeric ($num 2)) {//is_numeric                    ()///detect if the variable is a numeric or numeric string return value, True, false such as, ' + ' switch ($select) {//$select is the operator that came earlier                           Case ' + '://Depending on the syntax of the switch, the value in case is equal to the value in the switch bracket then the sentence after the case is executed, and then the $result = $num 1+ $num 2;                    Break                           Case '-': $result = $num 1-$num 2;                    Break                           Case ' * ': $result = $num 1* $num 2;                    Break Default:if ($num 2==0) {//Add a judgment that the divisor cannot be 0 echo "<script>alert (' Enter a divisor of 0 please re-enter ' </script> ';                                  }else{$result = $num 1/$num 2;                           Break }}}else{//echo when the user input is not a number, may be a string to prompt the user echo "<scri                    Pt>alert (' Not number of inputs ') </script> "; $num 1 = $num 2 = $result = "";//clear the contents of the form}}

Run results

When you enter the correct number

After clicking the = sign

The description value is not passed to the form in HTML.

Now you're going to set the value of the form

<input type = "text" name= "NUM1" value= "<?php echo $num 1?>" >//set value to num value after operation 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 $num 2?>" >

<input type = "Submit" name = "Submit" value= "=" >

<input type = "text" name= "result" value= "<?php echo $result?>" >

</form>

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 left blank when the user does not click the Submit button

Improved band code, add a else{to PHP code at the end

$num 1 = $num 2 = $result = "";

}

When you click a different operation, the middle operator is always +,

Code improvements

In HTML

<select name = "Select" >

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

Select has a property selected when it is set, it is selected by default so it has to be combined with the value of PHP to compare, True indicates that the selected false represents the unselected

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

Look at the results

When the user comes in for the first time

Description to set the default value in Selecte

Code

$select = "+"

Basic functionality has been completed

The total code

<! DOCTYPE html>
  • Related Article

    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.