Aim
To demonstrate hierarchical decision making in PHP using a nested if statement that classifies a student result from two independent conditions — marks and attendance.
Theory
An if statement executes its block only when its condition evaluates to true. Placing one if...else inside another builds a decision tree: the inner condition is evaluated only when the outer condition has already passed, which both documents the dependency between the checks and skips useless work.
Key points about conditions in PHP:
- Relational operators (
>=,>,<,<=,==,===,!=) all produce abool. ==compares after type juggling;===additionally requires identical types — prefer it whenever types matter.- Two boolean inputs give four combinations (a 2 × 2 truth table). Nesting lets each combination map to a different outcome, which a single flat
if ($a && $b)cannot:&&collapses all failure cases into oneelse. - Deep nesting (3+ levels) damages readability. Professionals flatten it with guard clauses (early
return/exit),elseifladders, or extracted functions. - Mandatory braces
{ }make the pairing between eachelseand itsifexplicit, avoiding the classic dangling else ambiguity.
Requirements
- XAMPP/WAMP with PHP 8.x (Apache) or PHP CLI
- Code editor such as VS Code
- Web browser or terminal
Procedure
- Start Apache from the XAMPP Control Panel.
- Create the folder
C:\xampp\htdocs\wbplab(reused for all practicals) and save the program asp02_nested_if.php. - Type the code from the snippet and save the file.
- Run it at
http://localhost/wbplab/p02_nested_if.php, or withphp p02_nested_if.phpin a terminal. - Re-run with the value pairs 82/60 and 30/90 for
$marks/$attendanceand record all three possible outcomes.
Explanation of the Code
$marks = 82and$attendance = 78are the two inputs being classified.- The outer condition
if ($marks >= 40)decides pass/fail. When it fails, control jumps straight to the outerelseand printsResult: FAIL.— the attendance test is never evaluated at all. - The inner condition
if ($attendance >= 75)runs only for passing students and splits them into eligible (PASS and ELIGIBLE for next semester) and attendance shortage branches. - Three mutually exclusive messages are produced from just two boolean tests — exactly the expressive power flat conditions lack.
Expected Output
With the given values (82 marks, 78% attendance) the browser shows exactly:
Result: PASS and ELIGIBLE for next semester.
Changing $attendance to 60 prints Result: PASS but attendance shortage.; changing $marks to 30 prints Result: FAIL. regardless of attendance.
🎯 Viva Questions
- When is the inner condition of a nested if evaluated? Only after the outer condition has evaluated to true.
- How does nesting differ from
if ($a && $b)?&&yields one combined failure branch; nesting can give every failure its own message. - What is short-circuit evaluation? With
&&, PHP skips the right operand entirely once the left is false. - Difference between
==and===?==compares after type juggling;===also demands identical types. - What is a guard clause? An early
return/exiton the failure path that removes one level of nesting. - How many outcomes can two nested boolean tests produce? Up to four; this program maps them onto three messages.
CO Mapping
CO1, CO2