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.
Though these are very common in most programming languages, let us examine the above list of statements with an example in the PHP script.
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.
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.
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.
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.
<?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:
else($alternate_title) {
$output = “Variable is not initialized”;
echo $output;
}
there is an error on else ….
we cannot pass parameter to else.
Yes, its a typo. It is meant to be elseif, I will fix the article. Thanks for your time.