Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

Practical 2: Nested If Statement

Lesson 3 of 35 in the free Web Based Programming Lab notes on Siksha Sarovar, written by Rohit Jangra.

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 a bool.
  • == 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 one else.
  • Deep nesting (3+ levels) damages readability. Professionals flatten it with guard clauses (early return/exit), elseif ladders, or extracted functions.
  • Mandatory braces { } make the pairing between each else and its if explicit, 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

  1. Start Apache from the XAMPP Control Panel.
  2. Create the folder C:\xampp\htdocs\wbplab (reused for all practicals) and save the program as p02_nested_if.php.
  3. Type the code from the snippet and save the file.
  4. Run it at http://localhost/wbplab/p02_nested_if.php, or with php p02_nested_if.php in a terminal.
  5. Re-run with the value pairs 82/60 and 30/90 for $marks/$attendance and record all three possible outcomes.

Explanation of the Code

  • $marks = 82 and $attendance = 78 are the two inputs being classified.
  • The outer condition if ($marks >= 40) decides pass/fail. When it fails, control jumps straight to the outer else and prints Result: 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

  1. When is the inner condition of a nested if evaluated? Only after the outer condition has evaluated to true.
  2. How does nesting differ from if ($a && $b)? && yields one combined failure branch; nesting can give every failure its own message.
  3. What is short-circuit evaluation? With &&, PHP skips the right operand entirely once the left is false.
  4. Difference between == and ===? == compares after type juggling; === also demands identical types.
  5. What is a guard clause? An early return/exit on the failure path that removes one level of nesting.
  6. How many outcomes can two nested boolean tests produce? Up to four; this program maps them onto three messages.

CO Mapping

CO1, CO2