PHP Program to Create Simple Calculator
if or switch to perform addition, subtraction, multiplication, or division, like if ($op == '+') { $result = $num1 + $num2; }.Examples
How to Think About It
if or switch. Perform the calculation accordingly. Handle special cases like division by zero to avoid errors.Algorithm
Code
<?php $num1 = 10; $num2 = 5; $op = '+'; if ($op == '+') { $result = $num1 + $num2; } elseif ($op == '-') { $result = $num1 - $num2; } elseif ($op == '*') { $result = $num1 * $num2; } elseif ($op == '/') { if ($num2 == 0) { echo "Error: Division by zero"; exit; } $result = $num1 / $num2; } else { echo "Invalid operator"; exit; } echo "Result: $result"; ?>
Dry Run
Let's trace the example where num1=10, num2=5, and op='+' through the code.
Assign values
num1 = 10, num2 = 5, op = '+'
Check operator
op is '+', so perform addition
Calculate result
result = 10 + 5 = 15
Output result
Print 'Result: 15'
| Step | Operation | Result |
|---|---|---|
| 1 | Assign num1=10, num2=5, op='+' | |
| 2 | Check if op == '+' | True |
| 3 | Calculate 10 + 5 | 15 |
| 4 | Print result | Result: 15 |
Why This Works
Step 1: Input values
We start by assigning the two numbers and the operator to variables so the program knows what to calculate.
Step 2: Check operator
Using if and elseif, the program decides which math operation to perform based on the operator.
Step 3: Handle division by zero
Before dividing, the program checks if the second number is zero to avoid an error and prints a message if so.
Step 4: Display result
Finally, the program prints the result of the calculation to the screen.
Alternative Approaches
<?php $num1 = 8; $num2 = 4; $op = '*'; switch ($op) { case '+': $result = $num1 + $num2; break; case '-': $result = $num1 - $num2; break; case '*': $result = $num1 * $num2; break; case '/': if ($num2 == 0) { echo "Error: Division by zero"; exit; } $result = $num1 / $num2; break; default: echo "Invalid operator"; exit; } echo "Result: $result"; ?>
<?php function add($a, $b) { return $a + $b; } function subtract($a, $b) { return $a - $b; } function multiply($a, $b) { return $a * $b; } function divide($a, $b) { if ($b == 0) { return null; } return $a / $b; } $num1 = 7; $num2 = 0; $op = '/'; switch ($op) { case '+': $result = add($num1, $num2); break; case '-': $result = subtract($num1, $num2); break; case '*': $result = multiply($num1, $num2); break; case '/': $result = divide($num1, $num2); break; default: echo "Invalid operator"; exit; } if ($result === null) { echo "Error: Division by zero"; } else { echo "Result: $result"; } ?>
Complexity: O(1) time, O(1) space
Time Complexity
The calculator performs a fixed number of operations without loops, so it runs in constant time O(1).
Space Complexity
It uses a few variables for input and output, so space usage is constant O(1).
Which Approach is Fastest?
All approaches (if-else, switch, functions) run in constant time; differences are mainly in readability and maintainability.
| Approach | Time | Space | Best For |
|---|---|---|---|
| If-Else | O(1) | O(1) | Simple quick checks |
| Switch Statement | O(1) | O(1) | Cleaner multiple condition checks |
| Functions | O(1) | O(1) | Reusable and maintainable code |