Control Structures
Use if-else statements, switch cases, and loops in PHP.
Control Structures is a free PHP Academy lesson on CoddyKit — lesson 4 of 5. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the PHP Academy learning path, one of 5 lessons in the course, and your progress syncs across the web and the CoddyKit app.
1
Control Structures
Welcome to the next step in PHP! In this lesson, you’ll learn about control structures, which allow you to make decisions and repeat actions in your programs. Let’s dive in!

2
What Are Control Structures?
Control structures determine the flow of your program. They let you:
- Make decisions using conditional statements.
- Repeat actions using loops.
Key Point: Control structures make your programs dynamic and adaptable.
3
If-Else Statements
The if statement executes a block of code if a condition is true. Use else or elseif to handle other conditions. Example:
<?php
$age = 18;
if ($age >= 18) {
echo "You are an adult.";
} else {
echo "You are a minor.";
}
?>
4
Switch Statements
The switch statement is used to execute one block of code out of many options. Example:
<?php
$day = "Monday";
switch ($day) {
case "Monday":
echo "Start of the work week!";
break;
case "Friday":
echo "Almost the weekend!";
break;
default:
echo "Just another day.";
}
?>
5
For Loops
The for loop is used to repeat a block of code a specific number of times. Example:
<?php
for ($i = 1; $i <= 5; $i++) {
echo "Iteration $i<br>";
}
?>
6
While Loops
The while loop executes a block of code as long as a condition is true. Example:
<?php
$count = 1;
while ($count <= 3) {
echo "Count: $count<br>";
$count++;
}
?>
7
Do-While Loops
The do-while loop is similar to the while loop but guarantees the code will run at least once. Example:
<?php
$count = 1;
do {
echo "Count: $count<br>";
$count++;
} while ($count <= 3);
?>
8
Foreach Loops
The foreach loop is designed for iterating over arrays. Example:
<?php
$colors = ["Red", "Green", "Blue"];
foreach ($colors as $color) {
echo "$color<br>";
}
?>
9
<?php
$number = 5;
if ($number > 10) {
echo "Greater than 10";
} else {
echo "10 or less";
}
?>
10
Great Job!
Congratulations! You’ve learned how to use control structures to make decisions and repeat actions in PHP. These tools are vital for creating dynamic and efficient programs. In the next lesson, we’ll explore arrays in more depth. Let’s keep coding!

Frequently asked questions
Is the “Control Structures” lesson free?
Yes — the full text of “Control Structures” is free to read here on the web, and the PHP Academy course includes 5 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the PHP Academy course, upgrade to CoddyKit PRO.
What will I learn in “Control Structures”?
Use if-else statements, switch cases, and loops in PHP. You practise PHP Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start PHP Academy?
No prior experience is required. PHP Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 5, so you can start here or from the beginning and move at your own pace.
How long does the “Control Structures” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this PHP Academy lesson?
Yes. Every PHP Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Variables and Constants
- Data Types
- Operators
- Control Structures
- Working with Arrays