The content of this PHP implementation is: The user enters two numbers, then selects one or more of the four operators of the subtraction, and displays the results of the calculation.
My idea is this: Create a form in HTML, These include: 1. Select the box, let the user choose subtraction operation, the selected result is saved in operation this array; 2. text box, let the user enter the number to be calculated, the result of input is saved in the array of NUM; 3. Submit button to submit form contents. The table is submitted directly to this PHP page with the POST method, where $_post[' operation ' stores the operator information, $_post[num '] stores the data to participate in the operation. In the PHP script, the operator is evaluated, the result is saved to the $msg array, and the output is $msg.
OK, just go to the code:
"; echo "Calculates the result as follows:". "
"; The echo $str of the foreach ($msg as $str).
"; echo $error;}? >
The operating interface is as follows:
Input 13, 12
If the operator is all selected:
Evaluation and Improvement:
After testing, found that there are some defects, such as: each time the data input, after submission, the results are displayed, but the page has been updated, the original input data will not be. The result of the improvement should be this: after each commit, the text box holds the last record, and the check box is not saved. Specific implementation, that is, in the text box set properties, this I am not particularly familiar with, and now too lazy to get it, just put it on the shelf.
As for the code, the PHP script used more variables, and the consumption of memory is correspondingly large, so like $result[], $num 1, $num 2 These variables are not called by another script, can be omitted, but in order to better extensibility, add functionality without drastically changing the original code, keep better.
Interested readers can go to http://www.beartracker.top/server1.php to test, welcome correction Oh ^_^
The above describes the PHP learning Note-A simple calculator script, including the PHP learning aspects, I hope to be interested in PHP tutorial friends helpful.