Control Structures in PHP

Control structures in PHP allow developers to control the flow of code execution. In This tutorial, we will discuss about conditional statements, loops, and jump statements in PHP with examples.

CodeEncape - Control Structures in PHP

Introduction to Control Structures in PHP

In PHP, control structures determine how the code will execute depending on conditions and loop for repeated content. To make code more dynamic, the code structure is most important for making conditions, execute code repeatedly, and stop the code execution.

Types of Control Structures in PHP

Common types include conditional statements, looping structures, and jump statements.

Conditional Statements

if Statement

Executes code if the condition is true.

Syntex

<?php
if (condition) {
    // Code to be executed if condition is true
}
?>

Example

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

In this example, we are checking the age of a person using the $age variable, which is set to 20.If $age is greater than 18, the code inside the if block will execute. However, if $age is set to 16, the condition will be false, so the if block will not run.

if...else Statement

Execute If block of code when condition is true, else block will be execute when the condition is false.

Syntex

<?php
if (condition) {
    // Code to be executed if condition is true
}else{
    // Code to be executed if condition is false
}
?>

Example

<?php
$age = 16;
if ($age >= 18) {
    echo "You are an adult.";
} else {
    echo "You are a minor.";
}
?>

In this example, we are checking the age of a person using the $age variable, which is set to 16.If $age is greater than 18, the code inside the if block will execute. However, if $age is set to 20, the condition will be false, so the else block will execute.

if...elseif...else Statement

Checks multiple conditions.

Syntex

<?php
if (condition) {
    // Code to be executed if condition is true
} elseif (condition) {
    // Code to be executed if condition is false and else if condition is true
} else {
    // Code to be executed if both condition is false.  
}
?>

Example

<?php
$marks = 75;
if ($marks >= 90) {
    echo "Grade: A";
} elseif ($marks >= 75) {
    echo "Grade: B";
} else {
    echo "Grade: C";
}
?>

In this example,We are checking student’s marks and assigns a grade. If $marks are 90 or above, it prints Grade: A; if they are 75 or above (but less than 90), it prints Grade: B; otherwise, it prints Grade: C.

switch Statement

Useful when comparing a single variable with multiple values.

Syntex

<?php
switch (variable) {
    case value1:
        // Code to execute if variable == value1
        break;
    case value2:
        // Code to execute if variable == value2
        break;
    ...
    default:
        // Code to execute if none of the cases match
}
?>

Example

<?php
$day = "Tuesday";
switch ($day) {
    case "Monday":
        echo "Start of the week.";
        break;
    case "Tuesday":
        echo "Second day of the week.";
        break;
    default:
        echo "Another day.";
}
?>

In this example,We are checking the $day. If it’s Monday, it prints Start of the week.; if it’s Tuesday, it prints Second day of the week.; otherwise, it prints Another day.

Looping Statements

In PHP, looping statements allow you to execute a block of code repeatedly until a specific condition becomes false. They are very useful for tasks such as iterating over arrays, printing sequences, or performing repetitive operations efficiently.

while Loop

Executes the code as long as the condition is true.When the condition becomes false code execution will be stopped

Syntex

<?php
while (condition) {
    // Code to be executed
}
?>

Example

<?php
$i = 1;
while ($i <= 5) {
    echo $i . " ";
    $i++;
}
?>

do...while Loop

In PHP, the do...while loop executes a block of code at least once, and then keeps repeating it as long as the condition evaluates to true.its different from a normal while loop, where the condition is checked before executing the block.

Syntex

<?php
do {
    // Code to be executed
} while (condition);
?>

Example

<?php
$number = 1;

do {
    echo "Number: $number <br />";
    $number++;
} while ($number <= 5);
?>

In above example, do...while Loop prints numbers 1 to 5. Before check the condition, it runs first, then checks the condition $number <= 5 before repeating.

for Loop

In PHP, the for loop is used when you know in advance how many times you want to execute a block of code.

Syntex

<?php
for (initialization; condition; increment/decrement) {
    // code to be executed
}
?>

Example

<?php
for ($i = 1; $i <= 5; $i++) {
    echo "Number: $i <br />";
}
?>

In above example, for Loop starts with $i = 1 , and will run as long as $i <= 5. After each iteration, $i increases by 1. and this way it prints number 1 to 5

foreach Loop

In PHP, the foreach loop is used to iterate over arrays. Without any counter, it automatically goes through each element and execute block of code inside the loop for every time.

Syntex

<?php
foreach ($array as $key => $value) {
    // code to be executed
}
?>

Example

<?php
$fruits = ["Apple", "Banana", "Mango"];

foreach ($fruits as $fruit) {
    echo "Fruit: $fruit <br />";
}
?>

Jump Statements in PHP

In PHP, Jump statements are used to change the normal flow of program execution. They allow you to jump out of loops, skip iterations, or stop script execution.

break statement

The break statement exits from a loop or switch statement and stop the execution of the loop.

continue statement

The continue statement skips the current iteration and moves to the next iteration in the loop.

exit / die

The exit or die statement terminates the execution of the code immediately. But difference it that, With die statement, you can optionally provide a message or status code.

Conclusion

In PHP, control structures are essential for controlling the flow of a program. They allow you to make decisions using conditional statements (if, if...else, switch), repeat tasks efficiently with loops (for, while, do...while, foreach), and manage program flow using jump statements (break, continue, exit, die). In short, the structures is key to writing dynamic, flexible, and efficient PHP programs.

Related Posts

Control Structures in PHP

Control structures in PHP allow developers to control the flow of code execution. In This tutorial,...

PHP Introduction

Basic introduction about what is php, benifits of php and how it runs on server and why it's widely...

Subscribe to our newsletter

Stay up-to-date about latest tech and new world. Unsubscribe at anytime!