Aim
To build a menu-driven calculator in PHP that dispatches the user's choice through a switch-case construct, including break, default and a division-by-zero guard.
Theory
A switch statement compares one subject expression against a series of case labels and starts executing at the first label that matches. Advanced points every examiner probes:
- Loose comparison:
switchmatches cases with==semantics, socase "1"would match integer1. PHP 8'smatchexpression is the strict (===) alternative that also returns a value and needs nobreak. - Fallthrough: without
break, execution continues into the next case's body. That is occasionally useful (grouping cases) but is a classic source of bugs — every case here ends withbreak. defaultruns when nothing matches; placing it last is a convention, not a requirement.- Menu-driven design is the standard pattern for console/CGI utilities: display numbered options, read a choice, dispatch with
switch, and loop until exit. It cleanly separates input, dispatch and action concerns. - Defensive programming: any arithmetic path that divides must guard the denominator (
$b != 0) because integer division by zero throws aDivisionByZeroErrorin PHP 8.
Requirements
- XAMPP/WAMP with PHP 8.x, or PHP CLI
- Code editor (VS Code), browser or terminal
Procedure
- Start Apache from the XAMPP Control Panel.
- Save the snippet as
p04_menu_switch.phpinC:\xampp\htdocs\wbplab. - Run
http://localhost/wbplab/p04_menu_switch.phpin the browser orphp p04_menu_switch.phpin a terminal. - Change
$bto0and observe the guarded division message; add a bogus choice such as9to$demoChoicesto seedefaultfire.
Explanation of the Code
$a = 20and$b = 5are the fixed operands;$demoChoices = [1, 2, 3, 4]simulates a user selecting every menu option in turn so the program is runnable without interactive input (in a real form, the choice would arrive via$_POST).- The menu itself is printed once with
echobefore the loop. - Inside
foreach,switch ($choice)dispatches:case 1prints the sum($a + $b),case 2the difference,case 3the product. case 4demonstrates the guard: it divides only when$b != 0, otherwise printsDivision by zero not allowed.— the check belongs inside the case so other operations remain unaffected.- Each case ends in
breakto prevent fallthrough, anddefaultcatches any invalid menu number.
Expected Output
After the four-line menu, the program prints four blocks: Choice: 1 / Addition: 25, Choice: 2 / Subtraction: 15, Choice: 3 / Multiplication: 100, Choice: 4 / Division: 4 — each block separated by a blank line.
🎯 Viva Questions
- What happens if
breakis omitted from a case? Execution falls through and runs the next case's statements too. - How does
switchcompare values? Loosely (==), with type juggling;matchin PHP 8 compares strictly. - When does
defaultexecute? Only when no case label matches the subject expression. - Why guard
case 4with$b != 0? Division by zero raisesDivisionByZeroError(fatal) in PHP 8. switchvsmatch— two differences.matchuses strict comparison and returns a value; it also has no fallthrough.- Why is a menu-driven structure preferred in lab utilities? It separates input, dispatch and action, making each operation independently testable.
CO Mapping
CO1, CO2