Php conditional statements-if, else, elseif

      No Comments on Php conditional statements-if, else, elseif

Various Php conditional statements are:

If statements

If statements are one of the most common constructs offering conditional statements.
Syntax:

if(expression)
{
statement;
}

Example:

<?php
$age=20;
if($age>=18)
{
echo "Person is adult";
}
?>

Else statements

If the condition in if blocks evaluates to false then else parts is executed.
Syntax:

if(expression)
{
statement;
}
else
{
statement;
}

Example:

<?php
$age=20;
if($age>=18)
{
echo "Person is adult";
}
else
{
echo "Person is not an adult;
}
?>

Elseif statements

If-else works nicley for either-or conditions,but elseif supports multiple condition checks.
Syntax:

if(expression)
{
statement;
}
elseif(expression)
{
statement;
}
else
{
statement;
}

Example:

<?php
$age=20;
if($age>=18)
{
echo "Person is adult";
}
elseif(age==18)
{
echo "Person is 18";
}
else
{
echo "Person is not an adult";
}
?>

Leave a Reply

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