Write a simple calculator program in PHP using switch case

How to make simple calculator program in php

Please Note some things if you are copying my.. make sure form action name same with your source code page..

Example:- my source code file name is calculator.php

Aim:- Design simple calculator using PHP

Software Requirements:- xampp server ( for Php running ), Code editor like, Vs Code, Notepad ++.

Process:- 

1-Create file and give the file name with .php extension ( calculator.php)

2- Copy my code, Otherwise learn about this project on YouTube and Try yourself to design this.

3-After completing your source code.. Open Xampp control panel

4-Start Apache and Mysql

5-Now, For accessing your result you have to give address of file location on any browser within your system like, Chrome, FireFox etc..

6- Your file location like:- http://localhost/1stproject/calculator.php 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Online Calculator</title>
</head>
<style>
    *{
        margin0;
        padding0;
    }
    h1{
        text-aligncenter;
    }
    form{
        width448px;
        font-size21px;
        height167px;
        marginauto;
        border2px solid red;
    }
    input{
        width250px;
        text-aligncenter;
    }
    select{
        width250px;
        text-aligncenter;
        font-size17px;
        letter-spacing3px;
    }
    .container{
        padding-left25px;
        margin-top13px;
    }

</style>
<body>
    <h1>Online Calculator</h1>
    <form action="calculator.php" method="post">
        <div class="container">
            <input type="text" name="no1">
            <label for="no1">Enter 1st value</label><br>
            <input type="text" name="no2"> 
            <label for="no2">Enter 2nd value</label><br>
            <select name="mainfun" id="">
                <option value="add" name="meth">Method:  +  -  *   /</option>
                <option value="add" name="add">Addition</option>
                <option value="sub" name="sub">Subtraction</option>
                <option value="mul" name="mul">Multiplication</option>
                <option value="div" name="div">division</option>
            </select><br>
            <input type="submit" name="subtn" value="Calculate">
        </div>
    </form>
      <?php
if(isset($_POST["subtn"]))
{
    $num1=$_POST["no1"];
    $num2=$_POST["no2"];
    $result;
    echo "<center><br> your 1st Enter number is  $num1 </center>";
    echo "<center><br> your 2nd Enter number is $num2 </center>";
    $action=$_POST["mainfun"];
    switch ($action) {
        case 'add':
            echo "<center><br>Your Addition value is</center>";
            $result=$num1+$num2;
            echo "<center><br>$result </center>";
            break;
        case 'sub':
            echo "<center><br>Your Subtraction value is </center>";
            $result=$num1-$num2;
            echo "<center><br>$result </center>";
            break;
        case 'mul':
            echo "<center><br>Your Multiplication value is </center>";
            $result=$num1*$num2;
            echo "<center><br>$result </center>";
            break;
        case 'div':
            echo "<center><br>Your Divisible value is </center>";
            $result=$num1/$num2;
            echo "<center><br>$result </center>";
            break;
        
        default:
            echo "Wrong Input";
            break;
    }
}
?>
</body>
</html>


Result:-

how to create a calculator using html and php

simple calculaote program in php 

Write PHP script for arithmetic Scientific calculator using form

scientific calculator in php source code

create calculator using function in php

php math calculator script

simple calculator program in php using if else

php code for calculator with buttons

Post a Comment

0 Comments