0
0
PHPprogramming~10 mins

If-else execution flow in PHP - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - If-else execution flow
Start
Evaluate condition
Execute if-block
End
Execute else-block
End
The program checks a condition. If true, it runs the if-block. Otherwise, it runs the else-block. Then it ends.
Execution Sample
PHP
<?php
$age = 18;
if ($age >= 18) {
    echo "Adult";
} else {
    echo "Minor";
}
?>
This code checks if age is 18 or more, prints 'Adult' if true, else prints 'Minor'.
Execution Table
StepCondition ($age >= 18)ResultBranch TakenOutput
118 >= 18Trueif-blockAdult
2End of if-else---
💡 Condition is true, so if-block runs and program ends after printing 'Adult'.
Variable Tracker
VariableStartAfter Step 1Final
$age181818
Key Moments - 2 Insights
Why does the program print 'Adult' and not 'Minor'?
Because at Step 1 in the execution_table, the condition ($age >= 18) is true, so the if-block runs and prints 'Adult'. The else-block is skipped.
What happens if the condition is false?
If the condition was false, the else-block would run instead, printing 'Minor'. This is shown by the 'Branch Taken' column in the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output at Step 1?
AMinor
B18
CAdult
DNo output
💡 Hint
Check the 'Output' column at Step 1 in the execution_table.
At which step does the program decide which block to run?
AStep 2
BStep 1
CBefore Step 1
DAfter Step 2
💡 Hint
Look at the 'Condition' and 'Branch Taken' columns in the execution_table.
If $age was 16, how would the 'Branch Taken' change in the execution_table?
AIt would change to 'else-block'
BIt would still be 'if-block'
CNo branch would be taken
DBoth blocks would run
💡 Hint
Refer to the 'Condition' and 'Branch Taken' logic in the execution_table.
Concept Snapshot
if (condition) {
  // code if true
} else {
  // code if false
}

Checks condition once.
Runs if-block if true.
Runs else-block if false.
Full Transcript
This PHP code uses an if-else statement to check if the variable $age is 18 or more. If true, it prints 'Adult'. Otherwise, it prints 'Minor'. The execution flow starts by evaluating the condition $age >= 18. Since $age is 18, the condition is true, so the if-block runs and outputs 'Adult'. The else-block is skipped. Variables remain unchanged during this process. This flow helps decide between two paths based on a condition.