PHP Control Structures
PHP control structures allow you to control the flow of your program. They help your code make decisions and repeat actions based on conditions. Without control structures PHP programs would always run line by line with no logic or flexibility.
What Are Control Structures in PHP
Control structures decide which part of the code should run and how many times it should execute. They are mainly used for decision making and looping.
If Statement
The if statement executes a block of code only when a condition is true.
Example
<?php
$age = 20;
if ($age >= 18) {
echo "You are eligible to vote";
}
?>
If Else Statement
The if else statement provides an alternative block when the condition is false.
Example
<?php
$age = 16;
if ($age >= 18) {
echo "You are eligible to vote";
} else {
echo "You are not eligible to vote";
}
?>
If Else If Else Statement
This structure is used when you need to check multiple conditions.
Example
<?php
$marks = 75;
if ($marks >= 90) {
echo "Grade A";
} elseif ($marks >= 60) {
echo "Grade B";
} else {
echo "Grade C";
}
?>
Switch Statement
The switch statement is used when you want to compare one value against multiple possible options. It often makes the code cleaner than using many if else statements.
Example
<?php
$day = "Monday";
switch ($day) {
case "Monday":
echo "Start of the week";
break;
case "Friday":
echo "Weekend is near";
break;
default:
echo "Regular day";
}
?>
For Loop
A for loop in PHP is a control structure used to repeat a block of code a specific number of times. It is used when you know in advance how many number of times the code should run.
for (initialization; condition; increment) {
// code to be executed
}
- Initialization → sets the starting point (e.g.,
$i =1) - Condition → loop runs while this is true (e.g.,
$i < 5) - Increment/Decrement → updates the counter each time (e.g.,
$i++)
Example
//for loop
<?php
for ($i = 1; $i <= 5; $i++) {
echo $i;
}
?>
While Loop
A while loop in PHP is a way to keep running a block of code again and again as long as a certain condition is true. Unlike a for loop, which is usually used when you already know how many times you want to repeat something, a while loop is better when you don’t know the exact number of times—it just keeps going until the condition changes.
while (condition) {
// code to be executed
}
- Condition → The loop runs as long as this evaluates to
true. - If the condition is
falseat the start, the loop won’t run at all.
Example
//while loop
<?php
$count = 1;
while ($count <= 3) {
echo $count;
$count++;
}
?>
Do While Loop
A do…while loop in PHP is very similar to a while loop, but with one important difference: it always runs the code at least once, even if the condition is false. This is because the condition is checked after the code block runs, not before.
do {
// code to be executed
} while (condition);
Example
//do while loop
<
?php
$x = 5;
do {
echo $x;
$x++;
} while ($x < 5);
?>
Foreach Loop
The foreach loop is specially designed to work with arrays.
Example
<?php
$colors = ["Red", "Green", "Blue"];
foreach ($colors as $color) {
echo $color;
}
?>
Break and Continue
The break statement stops the loop immediately.
The continue statement skips the current iteration and moves to the next one.
Example
<?php
for ($i = 1; $i <= 5; $i++) {
if ($i == 3) {
continue;
}
echo $i;
}
?>
Why Control Structures Matter
Control structures are essential for building real world PHP applications. They allow your program to respond to user input handle conditions process data and repeat tasks efficiently. Every login system form validation and database operation relies on control structures.
Click here for PHP Introduction
Click here for Variables and Data Types
Click here for Operators



5 Responses
[…] Link to PHP Control StructuresLink to PHP OperatorsLink to PHP Variables and Data Types […]
[…] Link to PHP Control StructuresLink to PHP OperatorsLink to PHP Variables and Data Types […]
[…] PHP Functions IntroductionPHP Variables and Data TypesPHP Control Structures […]
[…] Click here for PHP Control Structures […]
[…] PHP Arrays IntroductionPHP Array FunctionsPHP Control Structures […]