How to Perform Arithmetic Operation using Switch Case in PHP through HTML form ? Last Updated : 05 Jun, 2021 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 AdditionSubtractionMultiplicationDivision Comment More infoAdvertise with us S sravankumar_171fa07058 Follow Improve Article Tags : PHP HTML-Misc PHP-Misc Similar Reads PHP Tutorial PHP is a widely used, open-source server-side scripting language primarily designed for web development. It is embedded directly into HTML and generates dynamic content on web pages. It allows developers to handle database interactions, session management, and form handling tasks.PHP code is execute 9 min read Top 60+ PHP Interview Questions and Answers -2025 PHP is a popular server-side scripting language, widely known for its efficiency in web development and versatility across various platforms. PHP is extensively utilized by top companies such as Facebook, WordPress, Slack, Wikipedia, MailChimp, and many more due to its robust features and high perfo 15+ min read PHP Introduction PHP stands for Hypertext Preprocessor. It is an open-source, widely used language for web development. Developers can create dynamic and interactive websites by embedding PHP code into HTML. PHP can handle data processing, session management, form handling, and database integration. The latest versi 8 min read Design a Webpage for online food delivery system using HTML and CSS Creating your own online food delivery website is a great way to present a user-friendly platform for customers to browse and order food online. In this project, weâll design a simple yet effective food delivery website using HTML and CSS.What Weâre Going to Create...Weâll develop a straightforward 7 min read Version Control Systems Version Control Systems (VCS) are essential tools used in software development and collaborative projects to track and manage changes to code, documents, and other files. Whether you're working alone or as part of a team, version control helps ensure that your work is safe, organized, and easy to co 7 min read PHP Arrays Arrays are one of the most important data structures in PHP. They allow you to store multiple values in a single variable. PHP arrays can hold values of different types, such as strings, numbers, or even other arrays. Understanding how to use arrays in PHP is important for working with data efficien 5 min read PHP | Functions A function in PHP is a self-contained block of code that performs a specific task. It can accept inputs (parameters), execute a set of statements, and optionally return a value. PHP functions allow code reusability by encapsulating a block of code to perform specific tasks.Functions can accept param 8 min read Difference between HTTP GET and POST Methods HTTP (Hypertext Transfer Protocol) specifies a collection of request methods to specify what action is to be performed on a particular resource. The most commonly used HTTP request methods are GET, POST, PUT, PATCH, and DELETE. This article covers the 2 most common HTTP request methods, i.e. the GET 4 min read PHP Data Types In PHP, data types define the kind of value a variable can hold. PHP is a loosely typed language, meaning you donât need to declare the data type of a variable. It is automatically assigned based on the value. But it is important to understand data types because it is important for writing reliable, 4 min read PHP Operators In PHP, operators are special symbols used to perform operations on variables and values. Operators help you perform a variety of tasks, such as mathematical calculations, string manipulations, logical comparisons, and more. Understanding operators is essential for writing effective and efficient PH 8 min read Like