Home > Article > Backend Development > 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.
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.
Then, start to calculate the value entered in the text box
③Finally, output and display the calculated results:
Run:
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!