PHP If Else Statement

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 execution of a give PHP code based on conditions associated with it, like, other conditional statements, coming under PHP control structures, like, 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 else part

php_if_else

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

PHP if Statement

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

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

$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 variable $title is initialized or not.

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

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

else in PHP

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

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

$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 $title variable, in the next line itself we have called PHP unset() function to clear initialized string.

Now, if we apply isset() on $title using if statement, then, it will return false. After that, as like as we have seen in above example, 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 else part, instead of flatly executing it, when the if conditions are not satisfied. For example,

$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($alternate_title) {
$output =  "<h1>Variable is not initialized</h1>";
echo $output;
}

In the above code, though $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 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 same to apply conditions on else part.

Alternate Syntax for PHP if Statement

Instead of curly braces, an equal PHP command is used as an 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.

$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($alternate_title):
$output =  "<h1>Variable is not initialized</h1>";
echo $output;
endif;

Note:

  • No need to enclose 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 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.

This PHP code tutorial was published on July 25, 2013.

↑ Back to Top