else statement in PHP

“else” statement is an extension with “if” statement in PHP programming. If need to execute a block of statements when if statement return FALSE, we use else statement combine with if statement.

Here is the structure of a PHP program with else .
if (expression )
{
// execute statements if expression returns TRUE
}
else
{
// execute statements if expression returns FALSE
}

See the sample PHP program with else statement

<?php
$x = 10;
if ( $x < 5 )
{
 echo " value is less than 5";
}
else
{
 echo " value is greater than or equal to 5";
}
?>

Output of this program will be :
value is greater than or equal to 5

In this case if statement will return FALSE ( because 10 < 5 statement will return FALSE ) , then else statement will execute and code inside the else block will execute and will display the result “value is greater than or equal to 5”.

Note : else statement will come only with if statement

Share
Share
Share