How to Perform Arithmetic Operation using Switch Case in PHP through HTML form ? - GeeksforGeeks

05 Jun, 2021

We are going to perform the basic arithmetic operations like- addition, subtraction, multiplication, and division using PHP. We are using HTML form to take the input values and choose an option to perform particular operation using Switch Case.
Arithmetic Operations are used to perform operations like addition, subtraction etc. on the values. To perform arithmetic operations on the data we need at least two values.
Addition: It performs the sum of given numbers.
Subtraction: It performs the difference of given numbers.
Multiplication: It performs the multiplication of given numbers.
Division: It performs the division of given numbers.
Example:
Addition = val + val2 + ... + valn
Example: add = 4 + 4 = 8

Subtraction = val - val2 - ... - valn
Example: sub = 4 - 4 = 0

Multiplication = val1 * val2 * ... * valn
Example: mul = 4 * 4 = 16

Division = val1 / val2
Example: mul = 4 / 4 = 1
Program 1:
PHP
 

 
 










<?php  
 
$x = 15;
$y = 30;
 
$add = $x + $y;  
$sub = $x - $y;
$mul = $x * $y;
$div = $y / $x;  
 
echo "Sum: " . $add . "\n"; 
echo "Diff: " . $sub . "\n";
echo "Mul: " . $mul . "\n"; 
echo "Div: " . $div;
 
?>






Output:
Sum: 45
Diff: -15
Mul: 450
Div: 2
Using Switch Case: The switch statement is used to perform different actions based on different conditions.
Syntax:
switch (n) {
    case label1:
        code to be executed if n=label1;
        break;
    case label2:
        code to be executed if n=label2;
        break;
    case label3:
        code to be executed if n=label3;
        break;

    . . .
    
    case labeln:
        code to be executed if n=labellast;
        break;
    default:
        code to be executed if n is different from all labels;
}
Execution Steps:
Start XAMPP Server  
Open Notepad and type the below code and save the folder in path given in image  
Program 2:
PHP
 

 
 










<!DOCTYPE html>
<html>
<head>
    <title>GFG</title>
</head>
 
<body><center>
    <h1>
        ARITHMETIC OPERATIONS DEMO USING 
        SWITCH CASE IN PHP
    </h1>
 
    <h3>Option-1 = Addition</h3>
    <h3>Option-2 = Subtraction</h3>
    <h3>Option-3 = Multiplication</h3>
    <h3>Option-4 = Division</h3>
     
    <form method="post">
        <table border="0">
            <tr>
                <!-- Taking value 1 in an text box -->
                <td> <input type="text" name="num1"
                    value="" placeholder="Enter value 1"/>
                </td>
            </tr>
 
            <tr>
            <!-- Taking value 1 in an text box -->
            <td> <input type="text" name="num2" value=""
                    placeholder="Enter value 2"/>
                </td>
            </tr>
 
            <tr>
                <!-- Taking option in an text box -->
                <td> <input type="text" name="option" value=""
                    placeholder="Enter option 1-4 only"/>
                </td>
            </tr>
 
            <tr>
                <td> <input type="submit" name="submit"
                    value="Submit"/> 
                </td>
            </tr>
        </table>
    </form>
</center>
 
<?php
 
// Checking submit condition
if(isset($_POST['submit'])) {
 
    // Taking first number from the 
    // form data to variable 'a'
    $a = $_POST['num1'];
 
    // Taking second number from the 
    // form data to a variable 'b'
    $b = $_POST['num2'];
 
    // Taking option from the form
    // data to a variable 'ch'
    $ch = $_POST['option'];
 
    switch($ch) {
        case 1:
 
            // Execute addition operation 
            // when option 1 is given
            $r = $a + $b;
            echo " Addition of two numbers = " . $r ;
            break;
 
        case 2:
 
            // Executing subtraction operation
            // when option 2 is given
            $r = $a - $b; 
            echo " Subtraction  of two numbers= " . $r ;
            break;
 
        case 3:
 
            // Executing multiplication operation
            // when option 3 is given
            $r = $a * $b;
            echo " Multiplication of two numbers = " . $r ;
            break;
 
        case 4:
 
            // Executing division operation
            // when option 4 is given
            $r = $a / $b;
            echo " Division of two numbers = " . $r ;
            break;
 
        default:
 
            // When 1 to 4 option is not given
            // then this condition is executed
            echo ("invalid option\n");
    }
     
    return 0;
}
?>
</body>
</html>






Output: Open Web Browser and type localhost/gfg/code.php
Addition
Subtraction
Multiplication
Division
Attention reader! Don’t stop learning now. Get hold of all the important HTML concepts with the Web Design for Beginners | HTML  course.
adnanirshad158

HTML-Misc


PHP-Misc


HTML


PHP


PHP Programs


Web Technologies


Web technologies Questions

Writing code in comment? 
                                    Please use ide.geeksforgeeks.org, 
                                    generate link and share the link here.
                                
REST API (Introduction)
How to auto-resize an image to fit a div container using CSS?
Design a web page using HTML and CSS
How to Show Images on Click using HTML ?
HTML Course | First Web Page | Printing Hello World
How to pop an alert message box using PHP ?
How to execute PHP code using command line ?
PHP in_array() Function
How to convert array to string in PHP ?
How to delete an array element based on key in PHP?