📘 Lesson · Lesson 48
Calculator (Switch Case)
About
A simple calculator using switch case to choose the operation — a classic beginner PHP program.
Calculator Code
$a = 10; $b = 5; $op = "+";
switch ($op) {
case "+": echo $a + $b; break; // 15
case "-": echo $a - $b; break;
case "*": echo $a * $b; break;
case "/": echo ($b != 0) ? $a / $b : "Cannot divide by zero"; break;
default: echo "Invalid operator";
}
Summary
- switch picks the operation based on the operator.
- Always check for divide-by-zero before dividing.
परिचय
Operation चुनने के लिए switch case वाला simple calculator — एक classic beginner PHP program।
Calculator Code
$a = 10; $b = 5; $op = "+";
switch ($op) {
case "+": echo $a + $b; break; // 15
case "-": echo $a - $b; break;
case "*": echo $a * $b; break;
case "/": echo ($b != 0) ? $a / $b : "Cannot divide by zero"; break;
default: echo "Invalid operator";
}
सारांश
- switch operator के आधार पर operation चुनता है।
- Divide करने से पहले हमेशा divide-by-zero जाँचें।
💻 Live Code Editor
This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.