Home » Programming » PHP » Php If Else

How to Use if…else Conditional Statements in PHP [Examples]

Here’s a handy guide on using if/else/elseif conditional statements in PHP to execute code based on certain conditions.

if/else statements are found in pretty much all programming languages. Computer programs need to perform actions based on a user selection, or the result of a calculation, or the value received from a sensor – and if/else statements are how this is programmed into the system.

When building even the most basic programs, you’ll use them, so they’re worth getting to know.

PHP Syntax

There are several parts to an if statement in PHP – ifelse, and else if. Here’s a quick rundown on the purpose and syntax for each of them.

The if Statement

A simple if statement will execute code based on a single condition.

if (conditions) {
    // Actions to take only if conditions met
}

The else Statement

Add an else statement to an if statement to execute one block of code if a condition is true or a different block if the condition is false.

if (conditions) {
    // Actions to take only if conditions met
} else {
    // Actions to take only if conditions not met
}

The elseif Statement

Add any number of elseif statements to execute code based on multiple conditions.

if (conditions) {
    // Actions to take only if conditions met
elseif(conditions){
    // Actions to take only if the conditions preceding these ones are false, and these conditions are true
} else {
    // Actions to take only if all conditions not met
}

Conditions

Usually, you will pass a comparison to an if statement, evaluating whether a value is equal to, greater than, or less than another value.

The conditions will be evaluated for truthiness. If the condition is truthy (i.e., it evaluates to true), the code contained in the if statement will be executed, otherwise it is falsy, and the code will not be executed.

Most values in PHP will evaluate as being truthy, so it’s easier to list what is considered falsy:

  • Boolean FALSE values
  • Undeclared or undefined variables
  • null
  • 0
  • “0”
  • Empty arrays
  • Empty strings

So, you can pass pretty much any comparison or variable as a condition to your if statement, and it will be evaluated for truthiness and action taken accordingly.

Multiple conditions can be supplied separated by the and (&&) or or (||) operators.

if/elseif/else Examples

Here’s an example of a simple if statement. The text will only be printed using the echo command if the condition is met:

// Assign a random number between 0 and 15 to the variable $random
$random = rand(0, 15);

if($random > 5) {
    echo 'The random number is greater than 5';
}

Next, an example of an if/else statement. Different text will be displayed based on whether the condition is met or not:

if($random > 5) {
    echo 'The random number is greater than 5';
} else {
    echo 'The random number is not greater than 5';
}

if/elseif/else can be used to add as many conditions as required:

if($random > 5) {
    echo 'The random number is greater than 5';
elseif($random == 3){
    echo 'The random number is not greater than 5, but is equal to 3';
} else {
    echo 'The random number is not greater than 5 and not equal to 3';
}

You can add as many elseif statements:

if($random > 5) {
    echo 'The random number is greater than 5';
elseif($random == 3){
    echo 'The random number is not greater than 5, but is equal to 3';
elseif($random == 2){
    echo 'The random number is not greater than 5, but is equal to 2';
} else {
    echo 'The random number is not greater than 5 and not equal to 3 or 2';
}

and (&&) or or (||) operators can be used to compare multiple conditions at once:

if($random  == 6 || $random == 7) {
    echo 'The random number is either 6 or 7';
} elseif($random > 9 && $random > 13){
    echo 'The random number is greater than both 9 and 13';
}

The switch Statement- An Alternative

If you find yourself using a long string of if/else/elseif statements to perform actions based on the value of a single variable, you can try out the *switch statement instead – it’s a streamlined way to perform an action based on a variable’s value.

Click here to read more about the PHP switch statement.

SHARE:
Photo of author
Author
I'm Brad, and I'm nearing 20 years of experience with Linux. I've worked in just about every IT role there is before taking the leap into software development. Currently, I'm building desktop and web-based solutions with NodeJS and PHP hosted on Linux infrastructure. Visit my blog or find me on Twitter to see what I'm up to.

Leave a Comment