PHP If Else
PHP If Else
Sequential – this one involves executing all the codes in the order in which
they have been written.
Decision – this one involves making a choice given a number of options. The
code executed depends on the value of the condition.
PHP IF Else
If… then... else is the simplest control structure. It evaluates the conditions
using Boolean logic
When to use if… then… else
You have a block of code that should be executed only if a certain condition is
true
You have two options, and you have to select one.
If… then… else if… is used when you have to select more than two options
and you have to select one or more
<?php
if (condition is true) {
block one
else
block two
}
?>
HERE,
How it works The flow chart shown below illustrates how the if then… else control
structure works
Let’s see this in action The code below uses “if… then… else” to determine the
larger value between two numbers.
<?php
$first_number = 7;
$second_number = 21;
}else{
?>
Output:
21 is greater than 7
If no condition has been met then the default block of code is executed.
<?php
switch(condition){
case value:
break;
case value2:
default:
break;
?>
HERE,
How it works
The flow chart shown below illustrates how the switch control structure works
Practical example
The code below uses the switch control structure to display a message depending on
the day of the week.
<?php
$today = "wednesday";
switch($today){
case "sunday":
break;
case "wednesday":
break;
case "saturday":
break;
default:
break;
?>
Output:
Summary