Flow control determines whether a piece of code will be executed or not. There are two primary language constructs in PHP to handle flow control: ”if” and ”switch” statements.

If Statement

The if statement will execute code only if the statement is true.

In the code below, since $name is equal to ‘Anonymous’, the program outputs ‘Hi there!’.

$name = 'Anonymous';

if ($name == 'Anonymous')
{
    echo 'Hi there!';
}
Else If

The Else If statement is useful for handing more than one code path.

The below code has three separate paths for guests, users and administrators.

function explainRights($group)
{
    if ($group == 'guest')
    {
        echo 'You have very few access rights. Consider logging in.';
    }
    else if ($group == 'user')
    {
        echo 'You have basic rights.';
    }
    else if ($group == 'admin')
    {
        echo 'You have full administrator rights.';
    }
}

explainRights('user');   // Output: You have basic rights.
explainRights('admin');  // Output: You have full administrator rights.
explainRights('guest');  // Output: You have very few access rights. Consider logging in.
Else

Think of the Else statement as the “default” path for an if clause.

class CoffeeMaker
{
    public function testTemperature($temperature)
    {
        if ($temperature > 120)
        {

            return 'Too hot';
        }
        else if ($temperature < 90)
        {
            return 'Too cold';
        }
        else
        {
            return 'Just right';
        }
    }
}

$keurig = new CoffeeMaker;
echo $keurig->testTemperature(80) . "\n";    // Too cold
echo $keurig->testTemperature(130) . "\n";   // Too hot
echo $keurig->testTemperature(110) . "\n";   // Just right

Switch Statement

The switch statement is an alternative for multiple if statements against one variable.

Here is an example: explainRights() converted to switch:

function explainRights($group)
{
    switch ($group)
    {
        case 'guest': 
            echo 'You have very few access rights. Consider logging in.'; 
            break;
        case 'user':
            echo 'You have basic rights.';
            break;
        case 'admin':
            echo 'You have full administrator rights.';
            break;
    }
}

explainRights('user');   // Output: You have basic rights.
explainRights('admin');  // Output: You have full administrator rights.
explainRights('guest');  // Output: You have very few access rights. Consider logging in.

Any case statement without a ”break;” will cause the next case to be executed, until ”break;” is encountered or the end of the statement:

define('INVALID_USER', 1);
define('INVALID_PASS', 2);
define('LOGGED_IN',    3);

$loginStatus = INVALID_PASS;

switch ($loginStatus)
{
    case INVALID_USER: /* Skipped this break on purpose, so that both INVALID_USER and INVALID_PASS return the same error. */
    case INVALID_PASS: echo 'Invalid username/password combination. '; /* Oops!  Accidentally forgot this break! Now *anyone* can login. */
    case LOGGED_IN: echo 'Welcome!';
}

// Output: Invalid username/password combination. Welcome!

Because switch statements can almost always be rewritten as if statements and the dangers of forgetting to add ”break;” are so real, it is advisable to use them very sparingly, if ever.