PHP Introduction | Control Statements | Conditional Branching with if

Now that you know the basics of values and variables, let’s learn about control statements. Control statements are syntax used to control the flow of a program.

A program does not simply execute the written statements from top to bottom and then finish. Depending on what is needed, it changes the work it performs or repeats processing as many times as required. Syntax for controlling “how statements are executed” as needed is called a control statement.

There are several kinds of control statements. First, let’s look at the most important one: the if statement.

An if statement performs work in an either-or form depending on a condition. It is written as follows.

1. Execute processing if the condition is true.

if (condition) {
    ... processing when the condition is true ...
}

You can also use a colon (:) and endif instead of braces.

if (condition ):
    ... processing when the condition is true ...
endif;

2. Execute different processing depending on whether the condition is true or false.

if (condition) {
    ... processing when the condition is true ...
} else {
    ... processing when the condition is false ...
}

You can also use a colon (:) and endif instead of braces.

if (condition ):
    ... processing when the condition is true ...
else:
    ... processing when the condition is false ...
endif;

3. Execute different processing for multiple conditions.

if (condition1) {
    ... processing when condition1 is true ...
} elseif(condition2) {
    ... processing when condition2 is true ...

... continue elseif as needed ...
    
} else {
    ... processing when all conditions are false ...
}

You can also use a colon (:) and endif instead of braces.

if (condition1 ):
    ... processing when condition1 is true ...
elseif (condition2):
    ... processing when condition2 is true ...
    
... continue elseif as needed ...
    
else:
    ... processing when all conditions are false ...
endif;

In an if statement, the “condition” is set inside the parentheses. The word condition may sound difficult, but you can think of it as an expression or value that can be represented as “is this true or false?”

In the computer world, for example, a program checks values or variables with questions such as “Is the value of variable a 0?” or “Is the value of A greater than B?” and then executes different processing depending on whether that expression is true or false. This is the “condition” in an if statement.

After the parentheses containing the condition, write the work to perform. If there is only one statement, you can write it directly. If you want to write and execute multiple statements, surround them with {} and write the executable statements inside. This makes it clear that “the work to execute runs from here to here.” This {} section is called a block.

If you want to execute different work when the condition is false, write else after the processing for the true condition, then write the processing for the false condition. If it is a single statement, you can write it directly, but if there are multiple statements, wrap them in a block with {}.

When there are multiple conditions, you can use elseif to add other conditions. elseif can be used multiple times. else is optional, so you do not have to use it.

Let’s look at a simple example.

<?php
    $num = 12345;
    $namoji = $num % 2;
?>
<!DOCTYPE html>
<html lang="ko">
    <head> 
        <meta http-equiv="Content-Type"
            content="text/html; charset=UTF-8" /> 
        <title>sample page</title>
    </head>
    <body>
        <h1>Hello PHP!</h1>
        <div>
            <?php
                if ($namoji == 0) {
                    echo "{$num} is even.";
                } else {
                    echo "{$num} is odd.";
                }
            ?>
        </div>
    </body>
</html>

This example determines whether the value of the $num variable is even or odd and displays the result. You can tell whether a number is even or odd by dividing it by 2 and checking whether the remainder is 0. The remainder is obtained with $namoji = $num % 2;, and the displayed content changes depending on whether that value is 0 in the if statement.

Booleans and Comparison

The conditional expression used in the current if example may look unfamiliar.

if ($namoji == 0) { ... omitted ... }

It uses the symbol ==, which is two equal signs lined up. This is a comparison operator, a symbol used to compare the values on the right and left sides. The comparison operators are as follows.

Expression Description
A == B A and B are the same.
A != B A and B are different. It can also be written as A <> B.
A > B A is greater than B.
A >= B A is greater than or equal to B.
A < B A is less than B.
A <= B A is less than or equal to B.

Expressions using these symbols are used to indicate whether “that expression is true.” In this example, there was if ($namoji == 0). This checks whether $namoji is equal to 0, in other words, whether $namoji is 0. If it is true, the following processing is performed.

Computers often need to represent whether something is true. A value that can represent this either-or state of being true or false is called a Boolean.

This is represented by the reserved words true or false. For example, when “A == B is true,” the result is true; otherwise it is false. The comparison operators listed here also check two values and return true or false.

In other words, an if condition is “something represented as a Boolean.” The structure is that the processing is executed if the condition inside the parentheses is true.

Logical values appear very often in programming. They can be used with conditional operators, and they can also be used as ordinary variables. You can think of most situations that need an either-or value as using this kind of truth value.

Unlike numbers or text, this may be an unfamiliar value if you are not used to programming. Be sure to remember it at this stage.