ConditionalStatement-in PHP
ConditionalStatement-in PHP
Conditional statements are used to perform different actions based on different conditions.
if else statements: executes some code if a condition is true and another code if that
condition is false
program:
<!DOCTYPE html>
<html>
<body>
<?php
$a=20;
$b=10;
if($a>$b) // condition block
{
echo "a is greater than b.<BR>"; // true statement block block
}
else
{
echo "b is greater than a.";// false statement block
}
?>
</body>
</html>
else if else: - executes different codes for more than two conditions
program:
<!DOCTYPE html>
<html>
<body>
<?php
$age=65;
if($age<18)
{
echo "You are child";
}
elseif($age>=18 && $age<50)
{
echo "You are adult";
}
else
{
echo "You are senior citizen";
}
?>
</body>
</html>
program:
<!DOCTYPE html>
<html>
<body>
<?php
$age=71;
switch($age)
{
case ($age<18):
echo "You are child.";
break;
case ($age>=18 && $age<50):
echo "You are adult.";
break;
case ($age>=50 && $age<=70):
echo "You are senior citizen.";
break;
case ($age>70):
echo "You are super senior citizen.";
break;
default:
echo "you are in default block. Please provide proper input.";
}
?>
</body>
</html>