Open in App
Not now

# Calculator in PHP using If-Else Statement

• Last Updated : 22 Nov, 2022

PHP (Hypertext Preprocessor) is an open-source & server-side scripting language designed specifically for web development, where the scripts are executed on the server. It is free to download and use & can be easily embedded with HTML files. We can also write HTML codes in a PHP file. PHP calculator is used for performing basic mathematical operations like Addition, subtraction, multiplication, and division. In this article, we will see how to design the Calculator In PHP to perform basic arithmetic operations. For this, we will be using PHP, HTML, & JavaScript.

A simple calculator can be made using a PHP program that is able to add, subtract, multiply and divide, two operands entered by the user. The if-else statement will be used to create a calculator.

Approach: The following approach will be used to create the basic calculator:

We will declare two local variables say num1 and num2 for two numeric values. Then, we will enter the choice(Addition, subtraction, multiplication, or division). Then, it takes two numbers, num1, and num2 for further calculation. With the help of the if-else statement, it will jump to an operator selected by the user. Using the HTML table created the design where variables are holding the input field and the rest of them are filled with input buttons. Now, with the help of function res(), it displays the respective value of the button to the input field every time we click the button. The ans() function is used to set the value pressed from the keyboard to the same input field. All the calculations of the numbers entered can be done by both the “Enter” key and also with the help of the “=” button. The calculate() function will do the calculation with math.evaluate() function and then displays the final result to the input field. The clear_input() function is used whose work is to clear the input field.

Example: This example illustrates the PHP Calculator by implementing the basic if-else statement.

## PHP

 ` ` ` ` ` `  ` alert(\"Enter values.\");"``; ` `    ``} ` `} ` `?> ` ` `  ` ` `    `` ` ` `  `    `` ` ` ` ` `  ` `  ` ` `    `` ` `        ``
` `                `` ` `            ``
` `                ``  ` `            `` ` `                ``  ` `            `` ` `                ``  ` `            `` ` `                ``  ` `            ``
` `                ``  ` `            `` ` `                ``  ` `            `` ` `                ``  ` `            `` ` `                ``  ` `            ``
` `                ``  ` `            `` ` `                ``  ` `            `` ` `                ``  ` `            `` ` `                ``  ` `            ``
` `                ``  ` `            `` ` `                ``  ` `            `` ` `                ``  ` `            `` ` `                ``  ` `            ``
` `                ``  ` `            `` ` `                ``  ` `            `` ` `                ``  ` `            `` ` `                ``  ` `            ``
` `            `` ` `        `` ` `        `` ` `            `` ` `            `` ` `            `` ` `            `` ` `        `` ` `        `` ` `            `` ` `            `` ` `            `` ` `            `` ` `        `` ` ` `  `        `` ` `            `` ` `            `` ` `            `` ` `            `` ` `        `` ` `        `` ` ` `  `            `` ` `            `` ` `            `` ` `            `` ` `        `` ` `        `` ` `            `` ` `            `` ` `            `` ` ` `  `            `` ` `            `` ` `        `` ` `    `` ` ` `  `    `` ` ` ` ` `  ``

Output:

My Personal Notes arrow_drop_up
Related Articles