PHP Learning Notes--Simple calculator script PHP from getting started to mastering PHP learning website php100

Source: Internet
Author: User
Tags script php
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.

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