PHP Programming

Hands On No. 9 : Working with conditional statements PHP

Working with conditional statements PHP

if Statement
Syntax
if (condition) {
code to be executed if condition is true;
}

<?php
$t = rand(1,10);
if ($t < "3")
{
echo "Hi! ".$t;
}
?>

if...else Statement
Syntax
if (condition) {
code to be executed if condition is true;
}
else {
code to be executed if condition is false;
}

 

<?php
$t = rand(1,10);
if ($t < "3")
{
echo "Hi! ".$t;
}
else
{
echo "Hello ".$t;
}
?>

if...elseif...else Statement
Syntax
if (condition) {
code to be executed if this condition is true;
}
elseif (condition) {
code to be executed if first condition is false and this condition is true;
}
else {
code to be executed if all conditions are false;
}
  

<?php
$t = rand(1,10);
if ($t < "3")
{
echo "Hi! ".$t;
}
elseif ($t < "7")
{
echo "Hey ".$t;
}
else
{
echo "Hello ".$t;
}
?>