Create a Simple Calculator Using PHP #part1
What Will I Learn?
- Installation XAMPP (the environment needed for our php code to work)
- learning simple php code
- familiariz with the operation of a simple php code
- creating a simple calculator in php
Requirements
- Internet Connection
- XAMPP on your device
- Small knowledge about PHP
Difficulty
- Basic
Preparation
In this tutorial you will learn how to make a simple calculator using PHP. First of all we have to install XAMPP. I will present now how to download xampp step by step. if you already have XAMPP software on your computer, go to the heading "Calculator"
XAMPP installation
- Go to the website: XAMPP (click here)
- Click on the version that interests you
- Start downloading
- Open XAMPP Installation, and follow my steps:
- - Choose your language, and click Save.
- You will see a window like this:
- Now is the time to start everything. Click the first three "start" buttons, and your window should now look like this:
Now We have to go to the "htdocs" folder located in the xampp location.
For example, look at my path to this folder.
In this folder we create a file with the extension .php (for example "index.php")
We need to edit it with any editor and paste the code:
<><><><><><><><><><> <html> <body> <div align="center"> <font color="red" size="12"><h1>Calculator</h1></font> <form method="POST" action=""> <input type="text" name="number1" size="10"> <select name="sign"> <option>+</option> <option>-</option> <option>*</option> <option>/</option> </select> <input type="text" name="number2" size="10"> <input type="submit" value="Submit"> </form> <?php error_reporting(E_ALL ^ E_NOTICE); $number1 = $_POST['number1']; $number2 = $_POST['number2']; $sign = $_POST['sign']; $result = ""; switch ($sign) { case "+": $result = $number1+$number2; break; case "-": $result = $number1-$number2; break; case "*": $result = $number1*$number2; break; case "/": $result = $number1/$number2; break; } echo $result; ?> </div> </body> </html>
<><><><><><><><><><> Description of code operation:
<div align="center"> // setting the content in the "div" tag in the middle of the page <font color="red"><h1>Calculator</h1></font> // setting for the red color font and setting its size to "12", setting for the large header font.
<form method="POST" action=""> // creating a form, and processing it using POST
there are two methods; POST and GET.
You can read more about these two methods here:
POST & GET<input type="text" name="number1" size="10"> // Create a field in which numbers can be given <select name="sign"> // Create a drop-down list with options: <option>+</option> // 1st option (default) -> addition <option>-</option> // 2nd option -> subtraction <option>*</option> // 3rd option -> multiplication <option>/</option> // 4th option -> division </select> // end of the select operator
<input type="text" name="number2" size="10"> // Create a 2nd field in which numbers can be given <input type="submit" value="Submit"> // Create a button named "Submit" </form> // end of the form
<?php // we note that it's time to code in PHP now error_reporting(E_ALL ^ E_NOTICE); // ignoring errors $number1 = $_POST['number1']; // creation of variable number1 and sending it $number2 = $_POST['number2']; // creation of variable number2 and sending it $sign = $_POST['sign']; // creation of variable sign and sending it $result = ""; // creation of variable result switch ($sign) // creation switch
{ case "+": //we choose the "+" $result = $number1+$number2; // assigning a function variable that must be met break; case "-": //we choose the "-" function $result = $number1-$number2; // assigning a function variable that must be met break; case "*": //we choose the "*" function $result = $number1*$number2; // assigning a function variable that must be met break; case "/"://we choose the "/" function $result = $number1/$number2; // assigning a function variable that must be met break; } echo $result; // Displaying the result ?>
</div> // end of the div tag </body> // end of the body tag
Finally, when we finish coding, save our file in the same format as before, for example index.php
now we are entering the Internet, we write in url: localhost/index.php
We can enjoy our calculator.
Posted on Utopian.io - Rewarding Open Source Contributors
@vitusc, No matter approved or not, I upvote and support you.
Your contribution cannot be approved because it does not follow the Utopian Rules.
Suggestions:
You can contact us on Discord.
[utopian-moderator]