We use conditional statements in programming to apply different actions on different conditions. if
, if-else
, if-elseif-else
statements are conditional statements in PHP.
if
statement syntaxif (specific condition) {execute code if the condition is true;}
The if
condition checks if the age
is greater than 60
. If this condition is true
, it prints Retired
.
<?php$age = 61;if($age > 60){echo 'Retired'; // Display Retired if age is greater than 60}?>
if-else
statement syntaxif (specific condition) {execute code if the condition is true;}else{execute code if the condition is false;}
The if
condition checks if the age
is greater than 60
or not. If the condition is true
, then it will print Retired
. Otherwise, Not Retired
will be printed.
<?php$age = 40;if($age > 60){echo 'Retired'; // Display Retired if age is greater than 60} else{echo 'Not Retired'; // Display Not Retired if age is less than 60}?>
if-elseif-else
statement syntaxif (specific condition) {execute code if the condition is true;}elseif(condition){execute code if the previous condition is falseand this condition is true;}else{execute code if the above conditions are false;}
The if
condition checks if the age
is greater than 60
or not. If the condition is true
, it will print Retired
. Otherwise, if the age
is greater than 40
and not greater than 60
, Senior Employee
is printed. Lastly, if the first two conditions are false, Junior Employee
will be printed.
<?php$age = 40;if($age > 60){echo 'Retired'; // Display Retired if age is greater than 60} elseif($age > 40){echo 'Senior Employee'; // Display Senior Employee if age is greater than 40}else{echo 'Junior Employee';}?>