Conditional Code (Day-2)
Conditional Code (Day-2)
<?php
$c = 10;
$d = 10;
if ($c > $d) {
echo "c is bigger than d";
}
else if ($c==$d) {
echo "c is equal to d";
}
else {
echo "d is smaller than c"; } ?>
Result: c is equal to d
In the above example the if the condition "$c>$d" is true then the
message "c is bigger than d" is displayed, else the condition in the else if
that is "$c==$d" is evaluated if it is true the message "c is equal to d" is
displayed otherwise "d is smaller than c" is displayed
© Copyright 2016 Hidaya Institute Of Science and Technology
Switch Statement
Syntax:
switch ( <variable> ) {
case this-value:
Code to execute if <variable> == this-value
break;
case that-value:
Code to execute if <variable> == that-value
break;...
default:
Code to execute if <variable> does not equal the value
following any of the cases
break;
} © Copyright 2016 Hidaya Institute Of Science and Technology
Switch Statement
<?php
$x=2;
switch ($x) {
case 1:
echo “Number 1”;
break;
case 2:
echo “Number 2”;
break;
default:
echo “No number between 1 and 3”;
}
?>
© Copyright 2016 Hidaya Institute Of Science and Technology
Switch Statement
echo match (8.0) {
'8.0' => "Oh no!",
8.0 => "This is what I expected",
};
//> This is what I expected
match expression arms may contain multiple expressions separated by a comma. That is a logical
OR, and is a short-hand for multiple match arms with the same right-hand side.
<?php
$result = match ($x) {
// This match arm:
$a, $b, $c => 5,
// Is equivalent to these three match arms:
$a => 5,
$b => 5,
$c => 5,
};
?>
© Copyright 2016 Hidaya Institute Of Science and Technology
Match Expression
A special case is the default pattern. This pattern matches anything that wasn't previously
matched. For example:
<?php
$condition = 4;
$expressionResult = match ($condition) {
1, 2 => ”One and Two”,
3, 4 => ”Three and Four”,
default => ”Hidaya Trust - HIST”,
};
?>