• Simple calculator script using PHP

    Kindly follow below steps to create simple calculator script in PHP.

    Step 1 : calculate_forum.html

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    
    <html>
    <head><title>PHP Simple Calculator</title></head>
    <body>
    <form method="post" action="calculate.php">
    Enter First No.<input type="text" name="firstnum">
    Select operator:<select name="operator">
    <option>+</option>
    <option>-</option>
    <option>*</option>
    <option>/</option>
    </select>
    Enter Second No.<input type="text" name="secondnum">
    <input type="submit" name="calculate" value="Calculate">
    </form>
    </body>
    </html>

    Step 2 : calculate.php

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    
    <?php
    $first=$_POST['firstnum'];
    $second=$_POST['secondnum'];
    $operator=$_POST['operator'];
     if ($operator=="+") {$total=$first + $second;}
     if ($operator=="-") {$total=$first - $second;}
     if ($operator=="*") {$total=$first * $second;}
     if ($operator=="/") {$total=$first / $second;}
     echo $total;
    ?>

    Run calculate_form.html in your browser and enter a number in variable 1 and another number in variable 2. Then select a mathematical method, then click on calculate and then your answer will pop-up in a new page. The just click ‘Do Another’ to do another problem.

Comments on this post

Leave a Reply

  • Security Code :


    four − = 3