Objective: Learn how to control the flow of your PHP programs using conditional
statements and loops — the foundation for all decision-making in programming.
Control structures tell PHP when and how to execute code. They’re the traffic lights of
programming — deciding whether your code should stop, go, or loop back around.
Two main types:
if / elseBasic decision-making structure:
<?php
$age = 18;
if ($age >= 18) {
echo "You’re an adult.";
} else {
echo "You’re a minor.";
}
?>✅ Output: You’re an adult.
if / elseif / elseFor multiple possible outcomes:
<?php
$score = 85;
if ($score >= 90) {
echo "Grade: A";
} elseif ($score >= 80) {
echo "Grade: B";
} elseif ($score >= 70) {
echo "Grade: C";
} else {
echo "Grade: D or F";
}
?>🧠 You can chain as many elseif statements as needed.
switchSimplifies multiple conditions that compare the same value.
<?php
$day = "Monday";
switch ($day) {
case "Monday":
echo "Start of the week!";
break;
case "Friday":
echo "Weekend’s coming!";
break;
case "Saturday":
case "Sunday":
echo "It’s the weekend!";
break;
default:
echo "Just another weekday.";
}
?>🧠 Always include break; to stop the switch after a match.
Loops let you repeat code automatically — perfect for lists, tables, or calculations.
whileRepeats code while a condition is true.
<?php
$count = 1;
while ($count <= 5) {
echo "Count: $count <br>";
$count++;
}
?>✅ Output:
Count: 1
Count: 2
Count: 3
Count: 4
Count: 5do...whileRuns at least once before checking the condition.
<?php
$num = 1;
do {
echo "Number: $num <br>";
$num++;
} while ($num <= 3);
?>✅ Even if the condition is false, the loop runs once.
forCommonly used when you know how many times to loop.
<?php
for ($i = 1; $i <= 5; $i++) {
echo "Iteration $i <br>";
}
?>🧠 Structure: for (start; condition; increment)
foreachUsed specifically for arrays.
<?php
$colors = ["Red", "Blue", "Green"];
foreach ($colors as $color) {
echo "$color <br>";
}
?>✅ Output:
Red
Blue
Greenbreak and continuebreakStops a loop early:
<?php
for ($i = 1; $i <= 10; $i++) {
if ($i == 5) break;
echo "$i ";
}
// Output: 1 2 3 4
?>continueSkips one iteration:
<?php
for ($i = 1; $i <= 5; $i++) {
if ($i == 3) continue;
echo "$i ";
}
// Output: 1 2 4 5
?>Create odd-even.php:
<?php
$number = 7;
if ($number % 2 == 0) {
echo "$number is even.";
} else {
echo "$number is odd.";
}
?>✅ Try changing $number to test different results.
<?php
$count = 1;
while ($count <= 3) {
echo "This is loop number $count<br>";
$count++;
}
?>✅ Observe how it repeats automatically.
Create table.php:
<?php
$number = 5;
for ($i = 1; $i <= 10; $i++) {
echo "$number x $i = " . ($number * $i) . "<br>";
}
?>✅ Try different $number values.
<?php
$colors = ["red", "blue", "green", "yellow"];
foreach ($colors as $color) {
echo "Color: $color <br>";
}
?>✅ Add or remove colors to see the effect.
<?php
for ($i = 1; $i <= 10; $i++) {
if ($i == 4) continue;
if ($i == 8) break;
echo "$i ";
}
?>✅ Output should skip 4 and stop before 8.
Goal: Create an interactive number guessing game.
Create a file guess.php:
<?php
$secret = 7;
$guess = 0;
while ($guess != $secret) {
$guess = rand(1, 10); // random guess
echo "Trying $guess...<br>";
if ($guess == $secret) {
echo "🎉 Correct! The number was $secret.";
break;
}
}
?>✅ Refresh the page — watch it “guess” until it finds the number.
| # | Question | Options | Correct |
|---|---|---|---|
| 1 | Which statement is used for multiple conditions? | a) if / else · b) switch · c) while | b |
| 2 | Which loop guarantees at least one execution? | a) for · b) while · c) do…while | c |
| 3 | What does break do in a loop? | a) Ends the current iteration · b) Ends the loop · c) Skips to next loop | b |
| 4 | What is foreach used for? | a) Arrays · b) Numbers · c) Strings | a |
| 5 | What happens if continue is used? | a) Loop stops · b) Skips one iteration · c) Restarts the loop | b |
Objective: Combine your understanding of conditionals and loops to solve a famous coding problem.
Create a file fizzbuzz.php:
<?php
for ($i = 1; $i <= 20; $i++) {
if ($i % 3 == 0 && $i % 5 == 0) {
echo "FizzBuzz<br>";
} elseif ($i % 3 == 0) {
echo "Fizz<br>";
} elseif ($i % 5 == 0) {
echo "Buzz<br>";
} else {
echo "$i<br>";
}
}
?>✅ Output example:
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz🧠 This test is a rite of passage for all developers — and you’ve now written it in PHP!
odd-even.php → identifies even/oddtable.php → displays multiplication tableguess.php → number guessing worksfizzbuzz.php → correct FizzBuzz logic
🏁 Next Step: In Module 5, you’ll move into arrays and PHP’s built-in superglobals, learning how
to store multiple values and handle data from forms — essential for dynamic websites and WordPress development.