PHP If Else Statement Example


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

Though these are very common in most of the 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 the following code for an example of PHP if statement.

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 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 as like as 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.

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,

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 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 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 if…endif.

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


  • 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 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.

Leave A Reply

Your email address will not be published.