Using Control Structures in PHP Scripts

Control structures are essential elements in programming languages like PHP, allowing developers to control the flow of execution based on various conditions and to iterate through data efficiently using loops. Let's delve into how these control structures work with examples.

1. Conditional Statements in PHP:

Conditional statements enable PHP scripts to make decisions based on conditions. They allow the execution of specific blocks of code only when certain conditions are met.

a. The if Statement:

The if statement is the most basic conditional statement in PHP. It executes a block of code if a specified condition evaluates to true.

<?php
$age = 20;
if ($age >= 18) {
    echo "You are eligible to vote.";
}
?>

b. The else Statement:

The else statement works in conjunction with the if statement. It executes a block of code if the if condition evaluates to false.

<?php
$age = 15;
if ($age >= 18) {
    echo "You are eligible to vote.";
} else {
    echo "You are not eligible to vote.";
}
?>
 

 
c. The elseif Statement:

The elseif statement allows you to add additional conditions to the if statement. It provides an alternative when the original condition is false.
 

<?php
$age = 25;
if ($age < 18) {
    echo "You are a minor.";
} elseif ($age >= 18 && $age < 65) {
    echo "You are an adult.";
} else {
    echo "You are a senior citizen.";
}
?>
 
d. The switch Statement:

The switch statement evaluates an expression against multiple possible cases. It provides an alternative to multiple if-elseif-else statements.

<?php
$day = "Monday";
switch ($day) {
    case "Monday":
        echo "Today is Monday.";
        break;
    case "Tuesday":
        echo "Today is Tuesday.";
        break;
    default:
        echo "It's neither Monday nor Tuesday.";
}
?>
 
 

2. Loops in PHP:

Loops allow PHP scripts to iterate through data repeatedly. They are used to execute a block of code multiple times until a specified condition is met.

a. The for Loop:

The for loop executes a block of code a specified number of times. It consists of three parts: initialization, condition, and increment/decrement.

<?php
for ($i = 1; $i <= 5; $i++) {
    echo "The number is: $i <br>";
}
?>
 
b. The while Loop:

The while loop executes a block of code as long as a specified condition is true. It evaluates the condition before executing the block.

<?php
$i = 1;
while ($i <= 5) {
    echo "The number is: $i <br>";
    $i++;
}
?>
 
c. The do-while Loop:

The do-while loop is similar to the while loop but guarantees that the block of code is executed at least once before checking the condition.
 

<?php
$i = 1;
do {
    echo "The number is: $i <br>";
    $i++;
} while ($i <= 5);
?>
 
d. The foreach Loop:

The foreach loop is used to iterate over the elements of an array. It simplifies the process of looping through arrays.

<?php
$colors = array("Red", "Green", "Blue");
foreach ($colors as $color) {
    echo "Color: $color <br>";
}
?>
 
 

Practical Example:

Let's create a PHP script that generates a multiplication table for a given number using a loop:

<?php
$number = 5;
echo "<h2>Multiplication Table for $number</h2>";
echo "<table border='1'>";
for ($i = 1; $i <= 10; $i++) {
    echo "<tr><td>$number x $i</td><td>=</td><td>" . ($number * $i) . "</td></tr>";
}
echo "</table>";
?>



Practice Excercise Practice now