PHP If..Statement

  • PHP includes conditional statements like the If statement.
  • The If statement can be used to run different code depending on the value of the variable supplied to it.
  • It allows for conditional execution of code fragments.
Syntax
if (condition)
code to be executed if condition is true;
Example
<?php
if ($a > $b)
{
echo “a is bigger than b”;
$b = $a;
}
?>

If Else Statement

Example
<?php
if ($a > $b)
{
echo “a is greater than b”;
} else {
echo “a is NOT greater than b”;
}
?>

If… elseif/else if Statement

Example For elseif
<?php
if ($a > $b)
{
echo “a is bigger than b”;
}
elseif ($a == $b)
{
echo “a is equal to b”;
} else {
echo “a is smaller than b”;
}
?>
Example for else if
<?php
if($a > $b):
echo $a.” is greater than “.$b;
elseif($a == $b): // Note the combination of the words.
echo $a.” equals “.$b;
else:
echo $a.” is neither greater than or equal to “.$b;
endif;
?>

 

 

Leave a Reply

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