Now that you understand PHP Operators, it’s time to put them to real use. PHP Control Structures allow your scripts to make decisions and execute different code depending on certain conditions. Without control structures, every program would run the exact same way every single time.
This is Tutorial 4 of 10 in our PHP Basics for Beginners series. In this tutorial, you will learn how PHP Control Structures work using if, else, elseif, and switch statements with practical examples.
What are PHP Control Structures?
PHP Control Structures are blocks of code that control the flow of a program based on certain conditions. They allow your script to check a condition and then decide which block of code should run. This is one of the most important concepts in programming because almost every real application needs to make decisions.
For example, a login system needs to check if a password is correct, an online store needs to check if a product is in stock, and a form needs to check if all required fields are filled. All of these decisions rely on PHP Control Structures.
The if Statement
The simplest of all PHP Control Structures is the if statement. It runs a block of code only when a specified condition evaluates to true. If the condition is false, the code inside is simply skipped.
<?php
$age = 20;
if ($age >= 18) {
echo "You are an adult.";
}
?>
In this example, since the condition is true, the message will be displayed. If the age was below 18, nothing would be printed because there is no alternative path defined yet.
The if-else Statement
Most real-world PHP Control Structures need an alternative path when the condition is false. This is where the else statement becomes useful — it runs whenever the if condition does not match.
<?php
$age = 15;
if ($age >= 18) {
echo "You are an adult.";
} else {
echo "You are a minor.";
}
?>
Now the script always produces an output, regardless of whether the condition is true or false. This makes your PHP Control Structures far more practical for real applications.
The if-elseif-else Statement
Sometimes a single condition is not enough — you may need to check multiple possibilities. PHP Control Structures support the elseif keyword, which lets you chain several conditions together in sequence.
<?php
$marks = 75;
if ($marks >= 90) {
echo "Grade A";
} elseif ($marks >= 75) {
echo "Grade B";
} elseif ($marks >= 60) {
echo "Grade C";
} else {
echo "Grade F";
}
?>
PHP checks each condition from top to bottom and stops at the first one that matches. This pattern of PHP Control Structures is extremely common in grading systems, discount calculations, and access-level checks.
Nested if Statements
You can also place one if statement inside another. This is called nesting, and it allows PHP Control Structures to check more specific conditions after an initial condition is already satisfied.
<?php
$age = 22;
$hasLicense = true;
if ($age >= 18) {
if ($hasLicense) {
echo "You can drive.";
} else {
echo "You need a license first.";
}
} else {
echo "You are too young to drive.";
}
?>
The switch Statement
When you need to compare a single variable against many possible values, writing multiple elseif blocks can become messy. This is where the switch statement, another key part of PHP Control Structures, becomes much cleaner to use.
<?php
$day = "Tuesday";
switch ($day) {
case "Monday":
echo "Start of the work week.";
break;
case "Tuesday":
echo "Second day of the week.";
break;
case "Friday":
echo "Almost weekend!";
break;
default:
echo "A regular day.";
}
?>
The break keyword is essential in switch-based PHP Control Structures. Without it, PHP will continue executing the next case block even after finding a match, which usually leads to unexpected results.
When to Use if-else vs switch
Choosing the right PHP Control Structures depends on your situation. Use if-else when you are checking ranges, complex conditions, or multiple variables together. Use switch when you are comparing one single variable against several fixed, exact values, like checking a day name or a status code.
For more detailed technical documentation on conditional statements, you can refer to the official PHP control structures manual.
What’s Next?
You now understand how PHP Control Structures allow your scripts to make decisions using if, elseif, else, and switch. In the next tutorial, we will explore PHP Loops, which let you repeat code automatically instead of writing it manually over and over again.
If you missed the previous lesson, check out Tutorial 3: PHP Operators Guide, or explore all lessons in our PHP category.
Practice Exercise
Complete the following tasks to reinforce what you learned in this tutorial:
- Create a PHP file named
control-structures-practice.php - Write an if-else statement that checks if a number is positive, negative, or zero
- Write an if-elseif-else statement that assigns a grade (A, B, C, F) based on a marks variable
- Create a nested if statement that checks both age and citizenship status to determine voting eligibility
- Write a switch statement that prints a different message for each day of the week
- Add a default case in your switch statement for invalid day names
- Test your script by changing variable values and confirming the correct output each time
Bonus Challenge: Write a switch statement that takes a month number (1-12) and echoes the correct season (Winter, Spring, Summer, Fall) using grouped case statements without a break between related months.

