PHP If Else Statement

by Vincy. Last modified on July 6th, 2022.

If and Else statements are a kind of PHP conditional statements, since it applies one or more conditions upon a block of code, based on which the block is taken or non-taken for execution.

These statements handle the execution of a given PHP code based on conditions associated with it, like, other conditional statements, coming under PHP control structures, like, as PHP require/include, loops statements and etc.

And, the list of PHP constructs and syntax with the combination of if, else keywords are as follows.

  • if – Block will be taken for execution while true.
  • elseelse part if any will be taken for execution while if returns false.
  • else if – This is an alternative for PHP else statement, where it is required to apply conditions for executing the else part

Though these are very common in most programming languages, let us examine the above list of statements with an example in the PHP script.

PHP if Statement

This statement includes a single line or a bunch of code enclosed in curly braces, on which the if condition will be applied.

We can refer to the following code for an example of a PHP if statement.

<?php
$title = "PHP IF ELSE keywords";
if (isset($title)) {
    $output = "<h1>$title</h1>";
    echo $output;
}
?>

The above PHP code sample is started with a variable initialization. And then, if statement is used to apply conditions to check, whether the variable $title is initialized or not.

Obviously, if statement will return true, and, the code will be taken to add the $title into HTML h1 tags by using PHP variable interpolation.

If anything is wrong with variable initialization, then, if will return false, and, omit code follows.

else in PHP

This statement also contains a single line or a bunch of code, like if statements. But, it will be executed, once the condition on if statements go wrong, meaning that, once if returns false.

Now, let us have a glance at the following code which has two blocks, if and else.

<?php
$title = "PHP IF ELSE keywords";
unset($title);
if (isset($title)) {
    $output = "<h1>$title</h1>";
    echo $output;
} else {
    $output = "<h1>Variable is not initialized</h1>";
    echo $output;
}
?>

After initializing the $title variable, in the next line itself, we have called the PHP unset() function to clear initialized string.

Now, if we apply isset() on $title using if statement, then, it will return false. After that, we have seen in the above example, that it omits if block. But, instead of skipping from conditional execution, it will execute else part.

PHP elseif

This PHP conditional statement is used to apply conditions specifically for the else part, instead of flatly executing it, when the if conditions are not satisfied. For example,

<?php
$title = "PHP IF ELSE keywords";
$alternate_title = $title;
unset($title);
if (isset($title)) {
    $output = "<h1>$title</h1>";
    echo $output;
} elseif (isset($alternate_title)) {
    $output = "<h1>$alternate_title</h1>";
    echo $output;
} else if($alternate_title) {
    $output =  "<h1>Variable is not initialized</h1>";
    echo $output;
}
?>

In the above code, though the $title is cleared using unset(), one more condition is applied for elseif part to check whether $alternate_variable is set or not. If so, this block will display the output string to the browser after embedding it with HTML h1 tags. Otherwise, else part will be executed.

elseif can also be used as else if, where both are the same to apply conditions on the else part.

Alternate Syntax for PHP if Statement

Instead of curly braces, an equal PHP command is used as a delimiter for conditional blocks. For example, the conditional block of an if statement can be enclosed within ifendif.

The recent code sample we have seen for PHP elseif can be converted with this alternate syntax as follows.

<?php
$title = "PHP IF ELSE keywords";
$alternate_title = $title;
unset($title);
if (isset($title)) :
    $output = "<h1>$title</h1>";
    echo $output;
 elseif (isset($alternate_title)) :
    $output = "<h1>$alternate_title</h1>";
    echo $output;
 elseif ($alternate_title) :
    $output = "<h1>Variable is not initialized</h1>";
    echo $output;
endif;
?>

Note:

  • No need to enclose a single line which is the only one the if, else or elseif statements contains.
  • All conditional statements in PHP, with if and else, can apply a combination of two or more conditions.
  • All such statements listed in this article are expected to return either true or false.
  • PHP if and else statements can be nested.

Comments to “PHP If Else Statement”

  • Sonu Kumar says:

    else($alternate_title) {
    $output = “Variable is not initialized”;
    echo $output;
    }
    there is an error on else ….
    we cannot pass parameter to else.

Leave a Reply

Your email address will not be published. Required fields are marked *

↑ Back to Top

Share this page