Control Structures - potatoscript/php GitHub Wiki
Control Structures in PHP
Overview
Control structures in PHP allow you to control the flow of your program. They make it possible to execute specific blocks of code based on conditions, or repeat code multiple times. Control structures are crucial for implementing logic, making decisions, and creating efficient programs.
In this section, we will cover the following types of control structures:
- Conditional Statements
- Looping Statements
- Switch Statement
- Break and Continue
Conditional Statements
Conditional statements allow you to execute different blocks of code depending on whether a condition evaluates to true or false.
if Statement
The if
statement evaluates a condition, and if the condition is true, it executes a block of code.
<?php
$a = 10;
if ($a > 5) {
echo "The value of a is greater than 5.";
}
?>
if-else Statement
The if-else
statement provides an alternative block of code if the condition is false.
<?php
$a = 4;
if ($a > 5) {
echo "The value of a is greater than 5.";
} else {
echo "The value of a is not greater than 5.";
}
?>
if-elseif-else Statement
The if-elseif-else
statement allows multiple conditions to be checked in sequence.
<?php
$a = 10;
if ($a > 15) {
echo "The value of a is greater than 15.";
} elseif ($a > 5) {
echo "The value of a is greater than 5 but less than or equal to 15.";
} else {
echo "The value of a is less than or equal to 5.";
}
?>
Looping Statements
Looping statements are used to repeat a block of code multiple times based on certain conditions.
for Loop
The for
loop is used when you know how many times you want to execute a statement or a block of statements.
<?php
for ($i = 1; $i <= 5; $i++) {
echo "Iteration number: $i\n";
}
?>
while Loop
The while
loop executes a block of code as long as the specified condition is true.
<?php
$i = 1;
while ($i <= 5) {
echo "Iteration number: $i\n";
$i++;
}
?>
do-while Loop
The do-while
loop executes a block of code at least once and then repeats the loop as long as the specified condition is true.
<?php
$i = 1;
do {
echo "Iteration number: $i\n";
$i++;
} while ($i <= 5);
?>
foreach Loop
The foreach
loop is specifically used for iterating over arrays.
<?php
$fruits = array("Apple", "Banana", "Cherry");
foreach ($fruits as $fruit) {
echo "$fruit\n";
}
?>
Switch Statement
The switch
statement is used to execute one out of many possible blocks of code based on the value of an expression.
<?php
$day = "Monday";
switch ($day) {
case "Monday":
echo "Start of the work week!";
break;
case "Wednesday":
echo "Midweek day.";
break;
case "Friday":
echo "Almost the weekend!";
break;
default:
echo "Invalid day.";
}
?>
Explanation:
- The expression inside the
switch
is evaluated once. - Each
case
is compared to the result of the expression. - When a match is found, the corresponding block of code is executed.
- The
break
statement ends theswitch
block. Ifbreak
is omitted, PHP will continue checking the followingcase
statements even after a match is found.
Break and Continue
The break
and continue
statements allow you to alter the flow of loops and switch
statements.
break Statement
The break
statement is used to exit from a loop or switch
statement early.
<?php
for ($i = 1; $i <= 5; $i++) {
if ($i == 3) {
break; // Exit the loop when $i equals 3
}
echo "Iteration number: $i\n";
}
?>
continue Statement
The continue
statement is used to skip the current iteration of a loop and move to the next iteration.
<?php
for ($i = 1; $i <= 5; $i++) {
if ($i == 3) {
continue; // Skip the iteration when $i equals 3
}
echo "Iteration number: $i\n";
}
?>
Conclusion
In this section, we learned about the different control structures in PHP:
- Conditional Statements (
if
,if-else
,if-elseif-else
) - Looping Statements (
for
,while
,do-while
,foreach
) - Switch Statement (for handling multiple conditions)
- Break and Continue (for controlling loop execution)
These control structures provide the basic building blocks for decision-making and repetitive tasks in PHP. Understanding how and when to use them will make you much more effective in writing efficient, readable PHP code.