Web Development

This part is of the PHP Post Series dedicated to spread knowledge about PHP

There are two types of decision making in PHP.

  1. The if statements
  2. The switch statements

If Statement

The if statement consists of boolean expression(s) to be evaluated to make a decision.

Here’s some pseudocode

if (boolean expression) {
  // statements here will be executed if boolean expression evaluates to true.
}

If the boolean expression is true, then the statements in between the if statement is executed. If the boolean expression doesn’t evaluate to true, the statement in between the if statement doesn’t get executed and the next line after the if statement starts execution.

if (1 == 1) {
    // This gets executed
    echo "I did not know that!";
}

This will produce:

I did not know that!

If..Else Statement

In an if..else statement, whenever the boolean expression evaluates to false, the statement in the else block gets executed.

Here’s some pseudocode

if (boolean expression) {
  // statements here will be executed if boolean expression evaluates to true.
} else {
  // statements here will be executed if boolean expression evaluates to false.
}
if (1 == 2) {
    // This gets executed
    echo "I did not know that!";
} else {
    echo "I knew that!";
}

This will produce:

I knew that!