Basic calculator of PHP and basic calculator of PHP

Source: Internet
Author: User

Basic calculator of PHP and basic calculator of PHP

Design a computing function that completes computation and verifies unreasonable data and provides error messages.

Rule: the first number and the second number cannot be blank.

If the operator is/, the second number cannot be 0.

1 <? Php 2 header ('content-Type: text/html; charset = UTF-8 '); 3/* design a computing function, this function completes computation, verifies unreasonable data, and provides error messages. 4 rule: the first number. The second number cannot be null. 5. If the operator is/, the second number cannot be 0. */6 7 function jsq ($ num1, $ empty, $ num2) {8 // check data cannot be empty and prompt 9 if (! Isset ($ num1) |! Is_numeric ($ num1) {10 $ error = <ERROR11 <script> 12 alert ('the first number is invalid '); 13 </script> 14 ERROR; 15 return $ error; 16} 17 if (! Isset ($ num2) |! Is_numeric ($ num2) {18 $ error = <ERROR19 <script> 20 alert ('invalid second number'); 21 </script> 22 ERROR; 23 return $ error; 24} 25 26 if ($ scheme = "+") {27 $ result = $ num1 + $ num2; 28} elseif ($ region = "-") {29 $ result = $ num1-$ num2; 30} elseif ($ region = "*") {31 $ result = $ num1 * $ num2; 32} elseif ($ region = "/") {33 if ($ num2 = 0) {34 $ error = <ERROR35 <script> 36 alert ('second number cannot be 0'); 37 </script> 38 ERROR; 39 return $ error; 40} 41 $ result = $ num1/$ num2; 42} 43 return $ result; 44} 45 46 if ($ _ SERVER ['request _ method'] = "POST ") {47 $ res = jsq ($ _ POST ['num1'], $ _ POST ['num1'], $ _ POST ['num2']); 48} 49?> 50 51  


How to Use PHP code to write a calculator webpage

Php obtains an expression
Analyzes and processes expressions. You can look at the data structure book.
If you only use a simple calculator, you can obtain two numbers and one operator. That's easier.
I don't know how many functions you need to implement in this calculator.

How does php use the following code to perform addition, subtraction, multiplication, division, and remainder calculator?

Based on your code, I implemented it.
You can give it a try. If you have any questions, try again.
<! -- File 6-1.php: form data transmission --> <HTML> <HEAD> <TITLE> form data transmission </TITLE> </HEAD> <BODY> <? Php if (isset ($ _ POST ["tag"]) {$ addend1 = $ _ POST ["addend1"]; $ addend2 = $ _ POST ["addend2"]; $ method = $ _ POST ['method'];} else {$ addend1 = 0; $ addend2 = 0; $ method = '+';} switch ($ method) {case '+': $ result = $ addend1 + $ addend2; break; case '-': $ result = $ addend1-$ addend2; break; case 'X ': $ result = $ addend1 * $ addend2; break; case '/': $ addend2 = $ addend2 = 0? 1: $ addend2; $ result = $ addend1/$ addend2; break; case '%': $ addend2 = $ addend2 = 0? 1: $ addend2; $ result = $ addend1 % $ addend2; break; default: $ method = '+'; $ result = $ addend1 + $ addend2;}?> Enter two numbers in the following form for the value <form name = "form1" method = "post" action = "#"> <! -- The following is a hidden form, after receiving and using it, determine whether the page is submitted or after submission --> <input type = "hidden" name = "tag" size = "4" value = "1"> <input type = "text" name = "addend1" size = "4" value = "<? Php echo $ addend1;?> "> & Lt ...... remaining full text>

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.