What are Control Structures?
Control structures are the backbone of any programming language. They allow you to control the "flow" of your code, enabling you to make decisions and repeat actions. In this guide, you can explore fundamental control structures in PHP and try the interactive examples.
Note: These examples are JavaScript simulations that mimic PHP's behavior to provide an interactive learning experience in your browser. Running actual PHP code requires a server environment.
Make Decisions
Execute different blocks of code based on specific conditions. For example, show a "Welcome back!" message if a user is logged in, or a "Please log in" message if they are not.
Repeat Actions
Execute a block of code multiple times without rewriting it. For instance, display every item from a list of products, or count from 1 to 10.
Making Decisions with `if`, `elseif`, and `else`
The `if`, `elseif`, and `else` statements allow your program to execute specific blocks of code based on whether certain conditions are true or false. This is the primary way to introduce decision-making logic. Below is an interactive example based on assigning grades. Drag the slider to change the score and see how the conditional logic determines the final grade.
Interactive Grade Calculator
if ($grade >= 90) {
echo "You got an A!";
} elseif ($grade >= 80) {
echo "You got a B!";
} elseif ($grade >= 70) {
echo "You got a C.";
} else {
echo "Please see the teacher.";
}
Result:
Looping Through Arrays with `foreach`
The `foreach` loop is the most convenient way to iterate over each element in an array. It simplifies looping by automatically handling the array's length and current position. You can edit the data in the input boxes below and click "Run Loop" to see `foreach` in action with your own data.
Example 1: Getting Only the Value
$colors = ["Red", "Green", "Blue"];
foreach ($colors as $color) {
echo "<li>$color</li>";
}
Result (as a list):
Example 2: Getting Key and Value
$user = [
"name" => "John Doe",
"city" => "New York"
];
foreach ($user as $key => $value) {
echo "$key: $value<br>";
}
Result:
Repeating Actions with the `for` Loop
A `for` loop is used when you know exactly how many times you want a block of code to execute. It's controlled by three parts: an initializer (runs once at the start), a condition (checked before each iteration), and an incrementer (runs at the end of each iteration). Experiment with the values below to build your own loop.
Interactive Counter
for ($i = 1; $i <= 5; $i += 1) {
echo "The number is: $i <br>";
}
Result:
Conditional Looping with `while`
A `while` loop executes a block of code as long as a specified condition is true. Unlike a `for` loop, you don't need to know the exact number of iterations beforehand. The loop checks the condition before each iteration and stops when it becomes false. Be careful, as an incorrect condition can lead to an infinite loop!
Interactive Countdown
$countdown = 5;
while ($countdown > 0) {
echo "T-minus " . $countdown . "...<br>";
$countdown--;
}
echo "Liftoff!";