Home  >  Article  >  Backend Development  >  How to write a calculator in PHP

How to write a calculator in PHP

藏色散人
藏色散人Original
2019-10-15 09:10:442869browse

How to write a calculator in PHP

How to write a calculator in PHP?

Simple calculator

Code sharing on github: https://github.com/LVQINGDONG/learn_php Find the calculate branch to download

Open phpstorm and create a new calculate.html file in the directory, then go to the directory and change the suffix name to calculate .php.

①First write the html page structure.

How to write a calculator in PHP

Pay attention to the red line, because every time you click the submit button, the page will be refreshed, causing the value in the input page to become empty.

The red line part of the PHP code is to obtain the value of the input before the form is submitted (before the page is refreshed), and pass it to the value of the input value attribute after submission, so that the value of the input on the refreshed page retains the value before the previous submission.

if(isset($_GET["num1"])){} The function of this conditional judgment is to judge whether the data obtained by get has been submitted. If not, a warning will be reported:

Notice: Undefined index: num1 in C:\wamp\www\learn_php\calculate.php on line 22.

②Calculate the input value.

First, determine whether the values ​​entered in the two text boxes of the calculator are empty and whether they are numbers.

How to write a calculator in PHP

Then, start to calculate the value entered in the text box

How to write a calculator in PHP

③Finally, output and display the calculated results:

How to write a calculator in PHP

Run:

How to write a calculator in PHP

For more PHP related knowledge, please visit PHP Chinese website!

The above is the detailed content of How to write a calculator in PHP. For more information, please follow other related articles on the PHP Chinese website!

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