Learning PHP 101 – Conditionals and Control Flows

Conditionals and control flows are one of the most useful and common functions in PHP.

If statements

If statements allow you to make decisions within your code. Together with comparisons you can ask simple yes/no questions and provide different outcomes for each.

Lets say you need people to declare their age before you can display some content to them.

<?php
    $age = 27;

    if($age > 16) {
        echo "Some age restricted content";
    }
?>

In the example above the user must be greater than 16 years of age, other wise they will not be able to view the “age restricted content”.

If/Else Statements

If statements provide an excellent way to ask questions and provide a single answer, but sometimes we need to provide alternative answers according to the information given.

<?php
    $age = 27;

    if($age > 16) {
        echo "Some age restricted content";
    }
    else {
        echo "You are not permitted to view this content";
    }
?>

This example allows us to provide an alternative answer if the first condition is not true.

Else if

Sometimes we need to have multiple statements and to do that we need to use elseif in order to provide additional “questions”.

An example of that might be asking a series of questions in order to determine the shape of an object. Naturally we may start with a particular shape and work out way from there.

<?php
    $shape = "square";

    if($shape == "triangle") {
        echo "It is a triangle.";
    }
    elseif($shape == "square") {
        echo "It is a Square.";
    }
    elseif($shape == "circle") {
        echo "It is a circle.";
    }
    else {
        echo "I don't know what shape it is.";
    }
?>

With this statement we are essentially asking a question, “What shape is this object?”.

As the code runs it will first check to see if the variable $shape is equal to triangle and in this case it is not, so it will move on to the next statement to see if $shape is equal to square – which in this case is true and the output will echo “It is a Square”.

If our variable doesn’t match any of the conditions above it will execute whatever we have in the final statement “else“.

Switch Statements

Depending on the scenario using an (if/elseif/else) may not necessarily the best choice when testing conditions. Switch statements are another way to test conditions and are arguably a better choice for longer statements.

<?php
    $shape = "square";

    switch ($shape) {
        case "triangle":
            echo "It is a triangle.";
            break;
        case "square":
            echo "It is a Square.";
            break;
        case "circle":
            echo "It is a circle.";
            break;
        default:
            echo "I don't know what shape it is.";
    }
?>

Switch statements are often easier to read than long (if/elseif/else) statements and I would preference code readability over micro optimizations.

Switch statements are made up of a switch keyword which is a variable to check against followed by some curly braces.

switch (keyword) {}

Within the curly braces we have a case block.

case 1:
    echo "Case 1 is true";
    break;
default:
    echo "We couldn't find the truth!";

Each case block has a label (in this case: 1) and if the keyword matches the case label it will execute that block of code, in this case echo‘ing “Case 1 is true”.

In order to exit the switch statement we use break otherwise it will continue to run through the rest of the statements.

If all cases return false then the default case gets executed instead.

Multiple Cases With the same Result

Sometimes we need to bundle together different possible inputs where the result is the same. You could either write a long (if/elseif/else) like so:

<?php
    $var = 1;

    if($var == 1) {
        echo "It is a number between 1 and 3.";
    }
    elseif($var == 2) {
        echo "It is a number 1 and 3.";
    }
    elseif($var == 3) {
        echo "It is a number 1 and 3.";
    }
    else {
        echo "It is a number greater than 3";
    }
?>

When coding something you should consider the principals of DRY – “Don’t Repeat Yourself”. The snippet above clearly repeats itself and we can instead group together different cases where the outcome is the same.

<?php
    $var = 1;

    if($var == 1 || $var == 2 || $var == 3) {
        echo "It is a number between 1 and 3.";
    }
    else {
        echo "It is a number greater than 3";
    }
?>

Using the logical operator or (||) we can group our cases together which makes our code cleaner and more efficient. You can also do the same for switch statements.

<?php
    $var = 1;

    switch ($var) {
        case 1:
        case 2:
        case 3:
            echo "It is a number between 1 and 3.";
            break;
        default:
            echo "It is a number greater than 3";
    }
?>

Alternate Syntax

Sometimes you will see switch statements written like this:

<?php
    $var = 1;

    switch ($var):
        case 1:
        case 2:
        case 3:
            echo "It is a number between 1 and 3.";
            break;
        default:
            echo "It is a number greater than 3";
    endswitch;
?>

Instead of using curly brace syntax, some developers like to use an alternative syntax which uses endswitch, both the first and second syntax provide the same outcome, the latter exists for the purpose of syntactic sugar.

In computer science, syntactic sugar is syntax within a programming language that is designed to make things easier to read or to express. It makes the language “sweeter” for human use: things can be expressed more clearly, more concisely, or in an alternative style that some may prefer. – Wikipedia

Next: we will look at arrays and there many uses.

In Category: PHP tutorials

Matthew Horne

I am a web developer from the United kingdom who taught himself PHP and JavaScript and continues to build on those skills as well as learn new skills.

Show 0 Comments
No comments yet. Be the first.

Leave a Comment