🧩 Module 4: Control Structures
Objective: Learn how to control the flow of your PHP programs using conditional
statements and loops — the foundation for all decision-making in programming.
🔹 1. What Are Control Structures?
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:
- Conditional statements — make decisions.
- Loops — repeat actions.
🔹 2. Conditional Statements
if / else
Basic 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 / else
For 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.
switch
Simplifies 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.
🔹 3. Loops
Loops let you repeat code automatically — perfect for lists, tables, or calculations.
while
Repeats 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: 5
do...while
Runs 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.
for
Commonly 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)
foreach
Used specifically for arrays.
<?php
$colors = ["Red", "Blue", "Green"];
foreach ($colors as $color) {
echo "$color <br>";
}
?>
✅ Output:
Red
Blue
Green
🔹 4. break and continue
break
Stops a loop early:
<?php
for ($i = 1; $i <= 10; $i++) {
if ($i == 5) break;
echo "$i ";
}
// Output: 1 2 3 4
?>
continue
Skips one iteration:
<?php
for ($i = 1; $i <= 5; $i++) {
if ($i == 3) continue;
echo "$i ";
}
// Output: 1 2 4 5
?>
🧩 Hands-On Practice
Exercise 1: Odd or Even
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.
Exercise 2: While Loop Practice
<?php
$count = 1;
while ($count <= 3) {
echo "This is loop number $count<br>";
$count++;
}
?>
✅ Observe how it repeats automatically.
Exercise 3: Multiplication Table
Create table.php:
<?php
$number = 5;
for ($i = 1; $i <= 10; $i++) {
echo "$number x $i = " . ($number * $i) . "<br>";
}
?>
✅ Try different $number values.
Exercise 4: Colors Foreach Loop
<?php
$colors = ["red", "blue", "green", "yellow"];
foreach ($colors as $color) {
echo "Color: $color <br>";
}
?>
✅ Add or remove colors to see the effect.
Exercise 5: Skip and Stop
<?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.
🎯 Mini Project – Number Guessing Game
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.
🧾 Module 4 Quiz
| # | 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 |
💪 Challenge Task – FizzBuzz Generator
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!
🧾 Submission Checklist
- ✅
odd-even.php→ identifies even/odd - ✅
table.php→ displays multiplication table - ✅
guess.php→ number guessing works - ✅
fizzbuzz.php→ correct FizzBuzz logic - ✅ Quiz answers filled
🏁 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.