PHP If…Else

PHP Conditional Statements

Similar to other programming languages, PHP allows you to write code that performs different actions based on logical or comparative conditions evaluated at runtime. This involves creating expressions that result in either true or false, enabling specific actions based on these outcomes.

PHP provides several statements for decision-making:

  • The if statement
  • The if...else statement
  • The if...elseif...else statement
  • The switch...case statement

Each of these statements will be explored in detail in the following sections.

The if Statement

The if statement executes a block of code only if a specified condition evaluates to true. It's the simplest form of conditional statement in PHP, written as follows:

if(condition){
    // Code to execute
}

The example below demonstrates outputting "Have a nice weekend!" if today is Friday:

<?php
$d = date("D");
if($d == "Fri"){
echo "Have a nice weekend!";
}
?>

The if...else Statement

You can refine decision-making by providing an alternative choice with the addition of an else statement to the if statement. This construct allows executing one block of code if a specified condition evaluates to true and another block if it evaluates to false. The structure is as follows:

if(condition){
    // Code to execute if condition is true
} else{
    // Code to execute if condition is false
}

The example below will display "Have a nice weekend!" if today is Friday; otherwise, it will display "Have a nice day!"

<?php
$d = date("D");
if($d == "Fri"){
echo "Have a nice weekend!";
} else{
echo "Have a nice day!";
}
?>

The if...elseif...else Statement

The if...elseif...else statement is a special construct used to combine multiple if...else statements.

if(condition1){
    // Code to execute if condition1 is true
} elseif(condition2){
    // Code to execute if condition1 is false and condition2 is true
} else{
    // Code to execute if both condition1 and condition2 are false
}

The example below will display "Have a nice weekend!" if today is Friday, "Have a nice Sunday!" if today is Sunday, otherwise it will display "Have a nice day!"

<?php
$d = date("D");
if($d == "Fri"){
echo "Have a nice weekend!";
} elseif($d == "Sun"){
echo "Have a nice Sunday!";
} else{
echo "Have a nice day!";
}
?>

In the upcoming chapter, you'll discover how PHP's switch-case statement works.


The Ternary Operator

The ternary operator offers a concise method of writing if...else statements. It uses the question mark (?) symbol and requires three components: a condition to evaluate, a result if the condition is true, and a result if the condition is false.

To grasp its functionality, let's examine these examples:

<?php
if($age < 18){
echo 'Child'; // Display Child if age is less than 18
} else{
echo 'Adult'; // Display Adult if age is greater than or equal to 18
}
?>

You can achieve the same functionality more concisely using the ternary operator:

<?php echo ($age < 18) ? 'Child' : 'Adult'; ?>

In the example provided, the ternary operator chooses the value before the colon (i.e., 'Child') when the condition is true (when $age is less than 18), and it selects the value after the colon (i.e., 'Adult') when the condition is false.

 

Tip: While code written with the ternary operator can sometimes be challenging to decipher, it offers a concise method for writing if-else statements.


The Null Coalescing Operator PHP 7

PHP 7 introduces a new null coalescing operator (??) that serves as a shorthand for scenarios where you might otherwise use a ternary operator with the isset() function.

To better understand this concept, consider the following line of code. It retrieves the value of $_GET['name']; if it doesn't exist or is NULL, it defaults to 'anonymous'.

<?php
$name = isset($_GET['name']) ? $_GET['name'] : 'anonymous';
?>

You can do the same thing more simply using the null coalescing operator:

<?php
$name = $_GET['name'] ?? 'anonymous';
?>

As you can observe, the latter syntax is more concise and straightforward to write.