Control statements in PHP allow you to control the flow of execution of your program.
- Based on specific conditions, you can decide which block of code should be executed, repeated, or skipped.
- PHP provides several types of control statements, including conditional statements, loops, and flow control statements such as break and continue.
1. ) Conditional Statements:
Conditional statements are control statements that allows a program to make decisions and execute certain blocks of code only when specific conditions are met.
- Conditional statements evaluate expressions (conditions) that return either true or false and perform actions based on these evaluations.
Types of Conditional Statements:
- The if Statement
- The if-else statement
- The if-elseif-else statement
- The switch statement
a.) The if Statement:
- The if statement is used to execute a block of code if a condition is true. If the condition is false, it is skipped.
Syntax:
if (condition) {
// Code to be executed if the condition is true
}
Example:
<?php
$age = 20;
if ($age >= 18) {
echo "You are an adult.";
}
?>
- In this example, if $age is greater than or equal to 18, the message “You are an adult.” will be displayed.
2.) The if-else statement:
- The if-else statement allows you to execute one block of code if the condition is true and another block if the condition is false.
Syntax:
if (condition) {
// Code to be executed if the condition is true
} else {
// Code to be executed if the condition is false
}
Example:
<?php
$age = 15;
if ($age >= 18) {
echo "You are an adult.";
} else {
echo "You are a minor.";
}
?>
3.) The if-elseif-else Statement:
The if-elseif-else statement is used when you have multiple conditions to check. It allows you to test multiple conditions and execute code based on the first true condition.
Syntax:
if (condition1) {
// Code to be executed if condition1 is true
} elseif (condition2) {
// Code to be executed if condition2 is true
} else {
// Code to be executed if no conditions are true
}
Example:
<?php
$age = 25;
if ($age < 18) {
echo "You are a minor.";
} elseif ($age < 60) {
echo "You are an adult.";
} else {
echo "You are a senior citizen.";
}
?>
4.) The Switch Statement:
The switch statement is used to perform different actions based on different conditions. It is often used when you have many conditions to check for a single variable.
Syntax:
switch (expression) {
case value1:
// Code to be executed if the expression matches value1
break;
case value2:
// Code to be executed if the expression matches value2
break;
default:
// Code to be executed if no cases match
}
Example:
<?php
$day = 3;
switch ($day) {
case 1:
echo "Monday";
break;
case 2:
echo "Tuesday";
break;
case 3:
echo "Wednesday";
break;
default:
echo "Invalid day";
}
?>
2.) Looping Statements:
Loops are used to execute a block of code multiple times, based on a condition. A loop continues to execute as long as its condition remains true. Loops help in situations where repetitive tasks are required.
Types of Loops:
- for loop
- while loop
- do-while loop
- foreach loop
1.) for loop
The for loop is used when you know beforehand how many times you want to execute a statement or a block of statements.
Syntax:
for (initialization; condition; increment) {
// Code to be executed
}
Example:
<?php
for ($i = 1; $i <= 5; $i++) {
echo "Number $i<br>";
}
?>
2.) while loop
The while loop is used to execute a block of code as long as the specified condition is true. It checks the condition before executing the loop.
Syntax:
while (condition) {
// Code to be executed
}
Example:
<?php
$i = 1;
while ($i <= 5) {
echo "Number $i<br>";
$i++;
}
?>
3.) do-while loop
The do-while loop is similar to the while loop, except it checks the condition after executing the block of code. This ensures that the block is executed at least once, even if the condition is false.
Syntax:
do {
// Code to be executed
} while (condition);
Example:
<?php
$i = 1;
do {
echo "Number $i<br>";
$i++;
} while ($i <= 5);
?>
4.) foreach loop
The foreach loop is used to iterate over arrays. It is specifically designed to work with arrays and is used to access each element in an array.
Syntax:
foreach ($array as $key => $value) {
// Code to be executed
}
Example:
<?php
$colors = array("Red", "Green", "Blue");
foreach ($colors as $color) {
echo $color . "<br>";
}
?>
3.) Jumping Statements:
- break
- continue
1.) break Statement
The break statement is used to exit from a loop or a switch statement prematurely. It stops the execution of the loop or switch and transfers control to the next statement outside of the loop.
Example:
<?php
for ($i = 1; $i <= 5; $i++) {
if ($i == 3) {
break;
}
echo "Number $i<br>";
}
?>
2.) continue Statement
The continue statement is used to skip the current iteration of a loop and move on to the next iteration. It does not terminate the loop; it just skips the code below it for the current iteration.
Example:
<?php
for ($i = 1; $i <= 5; $i++) {
if ($i == 3) {
continue;
}
echo "Number $i<br>";
}
?>