PHP Control Structures: If else, Switch Case

⚡ Smart Summary

Control Structures and Loops decide the path a PHP program takes and repeat blocks of code efficiently. This walkthrough explains the if-else and switch decision structures, then covers the for, while, do-while, and foreach loops that competitors expect in this topic.

  • 🔀 Control Flow: A control structure chooses which code runs based on a condition, turning a straight sequence into decision-driven logic.
  • 🔤 If-Else: The if-else structure runs one block when a condition is true and a fallback block when it is false.
  • 🧮 Switch-Case: switch compares one value against several case labels and runs the matching block, or the default when none match.
  • 🔁 Loops Overview: Loops repeat a block of code, saving you from writing the same statements many times over.
  • 🔢 Counting Loops: for and while repeat while a condition holds, ideal when you know or track how many iterations are needed.
  • 📦 Foreach Loop: foreach walks through every element of an array without managing an index counter yourself.
  • 🤖 AI Assist: AI tools can refactor long if-else chains into switch or match statements and help fix infinite loops.

PHP Control Structures and Loops

What is a control structure?

Code execution can be grouped into categories as shown below.

  • Sequential – this one involves executing all the code in the order in which it has been written.
  • Decision – this one involves making a choice given a number of options. The code executed depends on the value of the condition.

A control structure is a block of code that decides the execution path of a program depending on the value of the set condition.

Let us now look at some of the control structures that PHP supports.

PHP IF Else

If… then… else is the simplest control structure. It evaluates the conditions using Boolean logic.

When to use if… then… else

  • You have a block of code that should be executed only if a certain condition is true
  • You have two options, and you have to select one.
  • If… then… else if… is used when you have to select more than two options and you have to select one or more

Syntax. The syntax for if… then… else is:

<?php
if (condition is true) {
    block one
} else {
    block two
}
?>

HERE,

  • if (condition is true)” is the control structure
  • block one” is the code to be executed if the condition is true
  • else is the fallback if the condition is false
  • block two” is the block of code executed if the condition is false

How it works. The flow chart shown below illustrates how the if… then… else control structure works.

PHP IF Else

Let us see this in action. The code below uses “if… then… else” to determine the larger value between two numbers.

<?php

$first_number = 7;

$second_number = 21;

if ($first_number > $second_number){

echo "$first_number is greater than $second_number";

}else{

echo "$second_number is greater than $first_number";

}

?>

Output:

21 is greater than 7

PHP Switch Case

Switch… case is similar to the if… then… else control structure.

It only executes a single block of code depending on the value of the condition.

If no condition has been met, then the default block of code is executed.

It has the following basic syntax.

<?php
switch(condition){

case value:

// block of code to be executed

break;

case value2:

// block of code to be executed

break;

default:

// default block code

break;

}
?>

HERE,

  • “switch(…){…}” is the control structure block code
  • “case value: case…” are the blocks of code to be executed depending on the value of the condition
  • “default:” is the block of code to be executed when no value matches the condition

How it works

The flow chart shown below illustrates how the switch control structure works.

PHP Switch Case

Practical example

The code below uses the switch control structure to display a message depending on the day of the week.

<?php

$today = "wednesday";

switch($today){

case "sunday":

echo "pray for us sinners.";

break;

case "wednesday":

echo "ladies night, take her out for dinner";

break;

case "saturday":

echo "take care as you go out tonight.";

break;

default:

echo "have a nice day at work";

break;

}

?>

Output:

ladies night, take her out for dinner

PHP Loops

Loops are control structures that repeat a block of code as long as a condition remains true. They save you from writing the same statement many times. PHP supports four loop types: for, while, do-while, and foreach. For a deeper walkthrough, see the dedicated PHP loop tutorial.

PHP For Loop

The for loop is used when you know in advance how many times the code should run. It combines the counter, the condition, and the increment on one line.

<?php
for ($i = 0; $i < 5; $i++) {
    echo $i;
}
?>

Output:

01234

PHP While Loop

The while loop repeats a block for as long as its condition stays true. The condition is checked before each iteration, so the body may run zero times.

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

Output:

01234

PHP Do-While Loop

The do-while loop is like the while loop, but it checks the condition after the body. This guarantees the block runs at least once.

<?php
$i = 0;
do {
    echo $i;
    $i++;
} while ($i < 5);
?>

Output:

01234

PHP Foreach Loop

The foreach loop is designed for arrays. It walks through every element without you managing an index counter.

<?php
$colors = array("red", "green", "blue");
foreach ($colors as $color) {
    echo $color . " ";
}
?>

Output:

red green blue

FAQs

Use switch when you compare one variable against many fixed values, as it is easier to read than a long if-else chain. Use if-else for ranges, complex conditions, or comparisons involving different variables.

break stops the loop completely and moves to the code after it. continue skips the rest of the current iteration and jumps to the next one, without ending the loop entirely.

match is a modern alternative to switch introduced in PHP 8. It returns a value, uses strict comparison, needs no break statements, and throws an error if no arm matches, making it safer and more concise.

Yes. AI can rewrite a long if-else chain as an equivalent switch or PHP 8 match expression, keeping the logic identical while making the code shorter and easier to read. Test the result afterward.

Yes. AI can inspect your loop condition and counter, spot a missing increment or a condition that never becomes false, and suggest the fix so the loop terminates as intended.

Summarize this post with: