Control flow structures

2

Control flow structures are the basic structures of a programming language. They let you to execute, avoid or repeat some parts of code on some conditions. PHP control structures are very similar to the same structures in C, C++, JavaScript, Java, Scala, and many other programming languages.

if-else

if control structure checks the given condition, and if it's true, then the control flow goes inside the if block, and if it's not — then the block is omitted. You can also supply it with optional elseif or else structures. elseif checks for an additional condition in case if the previous one was false, and go inside its block if its condition is true. A control flow will go inside else block if there were no true conditions above. You can have one if, one else and any number of elseif sections within the same if structure.

$is_black = false;
$is_white = true;

if($is_black)
{
    echo 'Black';
}
elseif($is_white)
{
    echo 'White';
}
else
{
    echo 'Colorful';
}

switch-case

switch is a sort of strict version of if-elseif-elseif-...-else structure. It checks the value inside switch and jumps to the corresponding case section. If no matching case was found then it goes to the default section:

switch($weather)
{
case 'rain':
    echo 'It is raining.';
    break;

case 'snow':
    echo 'It is snowing.';
    break;

case 'storm':
    echo 'It is a storm out there.';
    break;

default:
    echo 'The weather is clear.';
    break;
}

Notice that you should use break statement to leave the switch structure. And if you don't — the control flow will go further inside the switch:

switch($weather)
{
case 'rain':
case 'snow':
case 'storm':
    echo 'You should stay home.';
    break;

default:
    echo 'You could go for a walk.';
    break;
}

while

while structure is a loop. It checks the given condition, and if it's true, the control flow goes inside the while block, and if it's not — the control flow skips the block. Still if it's true then after reaching the end of the block the control flow goes back to the while condition, and repeat itself until the while condition is false:

$a = 100;
$b = 10;

while($b < $a)
{
    $a += 10;
    $b *= 2;

    echo "a = $a, b = $b<br />";
}

echo 'OK, b is finally not lesser than a';

do-while

This structure is almost the same as a while with a difference that the condition is being checked at the end of the block, not at the begining. This means that do-while block will be executed at least one time regardless of the condition state.

$a = 100;
$b = 10;

do
{
    $a += 10;
    $b *= 2;

    echo "a = $a, b = $b<br />";
}
while($b < $a);

for

for structure is also a loop and it has three statements. The frist one is executed first time the control flow reaches a for. Consider it as initialization statement. The second one is a condition on which the for block should be executed. And the last one is a statement that executed each time the control flow reaches the end of the for block. In the example below we initialize $n with 0 and execute for block until $n is not lesser than 10. Every time we hit the end of the block, $n is increased by 1:

for($n = 0; $n < 10; ++$n)
{
    echo $n . '<br />';
}

foreach

This structure is a special and simplified version of a for structure. It is used to iterate an array (or any other iterateable type that we'll discuss later). As you see, there are no special statements at all. And this is good. The lesser control you have — the lesser things you should care about:

$array = [1, 2, 3, 4, 5];

foreach($array as $value)
{
    echo $value . '<br />';
}

There is also associative array version of the foreach structure:

$people =
[
    'Ann'       => 19,
    'Jennifer'  => 21,
    'Nicole'    => 25,
    'Anastasia' => 29
];

foreach($people as $name => $age)
{
    echo "$name is $age<br />";
}

You should prefer foreach structure over for unless you really need to manipulate initialization, condition and post-iteration statements in some special way.

break and continue

You can use break and continue statements inside for or foreach. break is used to leave the structure right away. continue is used to jump to the next iteration of the structure:

$array = [1, 2, 3, 10000, 4, 5];

foreach($array as $value)
{
    if($value > 100)
    {
        echo "The big number is found, and it's $value!<br />";
        break;
    }
}

foreach($array as $value)
{
    if($value > 100)
    {
        echo 'This time we don\'t need the big number, so let\'s skip it!';
        continue;
    }

    echo "The small number is $value<br />";
}
Share this page:

Learning plan

The very basics of PHP variables
4. Arrays
The very basics of PHP arrays
The things you should know about PHP type system
6. Control flow structures
The basement of PHP control flow structures you should learn before continue
It's time to create your own PHP functions
How reference the same PHP values with different names
Most common and useful PHP built-in functions