0
0
PHPprogramming~10 mins

Nested conditional execution in PHP - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Nested conditional execution
Start
Check Condition 1
|Yes
Check Condition 2
|Yes
Execute Inner Block
End Inner Block
End Outer Block
End
No
Skip Inner Block
End
First, the outer condition is checked. If true, the inner condition is checked next. If both are true, the inner block runs. Otherwise, it skips.
Execution Sample
PHP
<?php
$age = 20;
if ($age >= 18) {
  if ($age < 21) {
    echo "You are an adult but not allowed to drink.";
  }
}
?>
This code checks if age is 18 or more, then checks if it is less than 21, and prints a message if both are true.
Execution Table
StepVariable $ageCondition 1 ($age >= 18)Condition 2 ($age < 21)ActionOutput
120TrueNot checked yetCheck outer condition
220TrueTrueCheck inner condition
320TrueTrueExecute inner blockYou are an adult but not allowed to drink.
420TrueTrueEnd
💡 Both conditions true, inner block executed, then program ends.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
$age2020202020
Key Moments - 2 Insights
Why is the inner condition only checked if the outer condition is true?
Because the inner if is inside the outer if block, so it runs only when the outer condition is true, as shown in steps 1 and 2 of the execution table.
What happens if the outer condition is false?
The inner condition is never checked and the inner block is skipped, so no output is produced. This is implied by the flow and the exit note.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of Condition 1 at step 1?
ANot checked
BTrue
CFalse
DUnknown
💡 Hint
Check the 'Condition 1' column in row for step 1 in the execution table.
At which step is the inner condition ($age < 21) evaluated?
AStep 3
BStep 1
CStep 2
DNever
💡 Hint
Look at the 'Condition 2' column in the execution table to see when it changes from 'Not checked yet' to a value.
If $age was 17, what would happen in the execution table?
ACondition 1 would be false, inner condition not checked, no output
BBoth conditions true, inner block executed
COnly inner condition checked
DOutput printed regardless of conditions
💡 Hint
Refer to the key moment about what happens if the outer condition is false.
Concept Snapshot
Nested conditional execution in PHP:
if (condition1) {
  if (condition2) {
    // code runs only if both true
  }
}
Inner condition runs only if outer is true.
Useful for step-by-step checks.
Full Transcript
This example shows nested conditional execution in PHP. First, the program checks if the variable $age is 18 or more. If yes, it then checks if $age is less than 21. Only if both conditions are true, it prints a message. The execution table shows each step: checking the outer condition, then the inner condition, then running the inner block. The variable $age stays the same throughout. Beginners often wonder why the inner condition is not checked if the outer is false; this is because the inner if is inside the outer if block. If $age was less than 18, the inner condition would never run and no output would appear. This step-by-step trace helps understand how nested if statements work in PHP.