Conditional statements in PHP enable the execution of different code blocks based on specific conditions. Let’s delve into some commonly used conditional statements.
The If Statement
The if
statement allows the execution of a code block if a given condition evaluates to true. The syntax is as follows:
<?php
$age = 15;
if ($age < 18) {
echo "You are a teenager";
}
?>
In this example, because the condition is true, the output would be:
You are a teenager
The If…Else Statement
The if...else
statement executes one code block if a condition is true and another code block if the condition is false. The syntax is:
<?php
$age = 25;
if ($age < 18) {
echo "You are a teenager";
} else {
echo "You are an adult";
}
?>
If the condition is false, the output would be:
You are an adult
The If…Elseif…Else Statement
This statement is used when multiple conditions are present. The syntax is:
<?php
$age = 10;
if ($age < 12) {
echo "You are a kid";
} elseif ($age < 18) {
echo "You are a teenager";
} else {
echo "You are an adult";
}
?>
In this example, the result would be:
You are a kid
Loops in PHP
Loops in PHP allow the execution of the same code block multiple times. PHP supports common loop types, such as for
and while
, as well as foreach
loops for iterating over arrays.
The For Loop
The for
loop is used when the number of iterations is known in advance. An example is:
<?php
for ($i = 0; $i < 5; $i++) {
echo "Iteration: $i <br>";
}
?>
The result of this loop would be:\
Iteration: 0
Iteration: 1
Iteration: 2
Iteration: 3
Iteration: 4
The While Loop
The while
loop executes a block of code as long as a test expression remains true. Example:
<?php
$counter = 0;
while ($counter < 3) {
echo "Counter: $counter <br>";
$counter++;
}
?>
The result remains the same as before:
Counter: 0
Counter: 1
Counter: 2
The do…while Loop
The do...while
loop is employed when there is a need to execute a block of code at least once, and then continue execution as long as a specified test expression remains true.
An example of using the do...while
loop is illustrated below:
<?php
$counter = 1;
do {
echo "Loop Number: $counter <br>";
$counter++;
} while ($counter <= 3);
?>
In this scenario, the initial loop number is 1 because the first echo statement is executed before the variable incrementation. The result would be:
Loop Number: 1
Loop Number: 2
Loop Number: 3
The foreach Loop
The foreach
loop is specifically designed for iterating through arrays. During each iteration, the array element is treated as a value, and the array pointer advances by one, facilitating the processing of the next element.
Consider the following example of using the foreach
loop:
<?php
$numbers = [1, 2, 3];
foreach ($numbers as $number) {
echo "Loop Number: $number <br>";
}
?>
In this instance, the initial loop number is 1, as the first echo statement is executed after processing the first array element. The output would be:
Loop Number: 1
Loop Number: 2
Loop Number: 3
Exploring these loop types in PHP provides you with versatile tools for handling repetitive tasks and iterating through arrays efficiently. As you continue your PHP journey, experiment with different loop scenarios to deepen your understanding of their functionality.