If statement enable programmer to execute a script only when a condition is true.
And Else enable to quickly execute a script when the condition fails.

PHP Code:

$login_name = "Fedrick" ;
if ( $login_name == "Fedrick" ) {
echo "Hello ! You are Fedrick"; // wil be displayed only if condition is true
} else {
echo "Hello ! You are not Fedrick";// wil be displayed only if condition is false
}

This may also be writed like this:

$login_name = "Fedrick" ;
if ( $login_name == "Fedrick" ) 
echo "Hello ! You are Fedrick"; // wil be displayed only if condition is true
else
echo "Hello ! You are not Fedrick";// wil be displayed only if condition is false

“{” and “}” can be deleted because there is only one line in if and else statements. If you want to create multiple lines of script, so then you should use”{” and “}”:

PHP Code:

$login_name = "Fedrick" ;
if ( $login_name == "Fedrick" )
{
echo "Hello ! You are Fedrick"; // wil be displayed only if condition is true
echo "You are wellcome"; 
} else 
echo "Hello ! You are not Fedrick";// wil be displayed only if condition is false

Here the “{” and “}” are necessary in the if statement, but not necessary in the else one as else statement has only one line of code

Leave a Reply

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