Create a Simple Calculator Using PHP #part1

in #utopian-io7 years ago (edited)

1calc.png2calc.png

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

XAMPP installation

  1. Go to the website: XAMPP (click here)
  2. Click on the version that interests you
    xamp.png
  3. Start downloading

as.png

  1. Open XAMPP Installation, and follow my steps:
    - 2121.png
    • 3131.png
    • 4141.png
    • 5151.png
    • 6161.png
    • 7171.png
    • 8181.png
    • Choose your language, and click Save.
    • sa.png
    • You will see a window like this:
    • wd.png
    • Now is the time to start everything. Click the first three "start" buttons, and your window should now look like this:
    • sc.png

      Now We have to go to the "htdocs" folder located in the xampp location.
      For example, look at my path to this folder.
      htdocs.png

      In this folder we create a file with the extension .php (for example "index.php")
      daw.png

      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

Sort:  

@vitusc, No matter approved or not, I upvote and support you.

Your contribution cannot be approved because it does not follow the Utopian Rules.

  • Tutorials must be technical instructions that teach non-trivial aspects of an Open Source project.

Suggestions:

  • You should really teach in your tutorials instead of just showing simple examples. Utopian expects tutorials to be technical and instructive, do-what-I-do tutorials are not allowed.

You can contact us on Discord.
[utopian-moderator]

Coin Marketplace

STEEM 0.18
TRX 0.15
JST 0.029
BTC 63439.39
ETH 2545.40
USDT 1.00
SBD 2.66