CodesJava

Easy learning with example program codes

PHP If Else


PHP If Else statement is a conditional statement, which can be used in various forms to check a condition or multiple conditions and to perform the specified action or actions, if the particular condition is true. These are:

if :
If statement is used to perform an action if a single condition is true.

Syntax:

if (condition) 
{
	Execute this code if condition is true; else nothing to be executed;
}

if…else:
If else statement is used to perform an action if a single condition is true and to perform another action if that condition is false.

Syntax:

if (condition) 
{
    Execute this code if condition is true;
} 
else 
{
    Execute this code if condition is false;
}

if…elseif….else:

If elseif else statement is used to perform different actions for different conditions.

Syntax:

if (condition) 
{
    Execute this code if this condition is true;
} 
elseif (condition) 
{
    Execute this code if this condition is true;
}
else 
{
    Execute this code if all the conditions are false;
}

Example 1: Example of if statement

<!DOCTYPE html>
<html>
<body>
 
<?php
$sum = 200;
 
if ($sum <= 200) 
{
    echo "You Lose!";
}
?>
 
</body>
</html>

Output

You Lose!

Example 2: Example of if..else statement

<!DOCTYPE html>
<html>
<body>
 
<?php
$sum = 300;
 
if ($sum <= "200")
{
    echo "You Lose!";
}
else 
{
    echo "You Won!";
}
?>
 
</body>
</html>

Output

You Won!

Example 3: Example of if..elseif..else statement

<!DOCTYPE html>
<html>
<body>
 
<?php
$sum = 200;
 
if ($sum < "200") 
{
    echo "You Lose!";
}
elseif($sum = "200") 
{
    echo "Match Draw!";
}
else 
{
    echo "You Won!";
}
?>
 
</body>
</html>

Output

Match Draw!
Posted in PHP   
Sign Up/ Sign In
Ask a Question


Copyright © 2018 CodesJava DMCA.com Protection Status SiteMap Reference: Java Wiki