Hi guys, in this tutorial we will learn PHP if else elseif Statements.
When writing programs / scripts, there will be situations where certain conditions are met only if you want to run a specific statement. In such situations we use conditional statements.
PHP we have the following 3 conditional statements:
- if statement
- if...else statement
- if...elseif...else statement
if statement:
if statement condition is a true executes code
Syntax:
if(condition){
//*********//
}
//*********//
}
Example:
<?php
$date = '1990';
if ($date == '1950') {
echo "Happy Birthday Johan";
}
if ($date == '1990') {
echo "Happy Birthday Andy";
}
?>
Output:
Happy Birthday Andy
if...else Statement
if...else Statement condition is s true executes if code or condition is s false executes else part
Syntax:
if(condition){
//*********//
}else{
//*********//
}
//*********//
}else{
//*********//
}
Example:
<?php
$number = 10;
if($number%2 == 0){
echo "$number is a even number";
}else{
echo "$number is a odd number";
}
?>
Output:-
10 is a even number
if...elseif....else Statement:
if have a more than two conditions then use if...elseif....else Statement, if one condition is a false check elseif conditions and all conditions is a false executes else part.
Syntax:
if(condition){
//*********//
}elseif(condition){
//*********//
}else{
//*********//
}
//*********//
}elseif(condition){
//*********//
}else{
//*********//
}
Example:
<?php
$per=69;
if ($per >= 70){
echo "Distinction";
}else if ($per >= 60){
echo "First Class";
}else if ($per >= 50){
echo "Secund Class";
}else if ($per >= 35){
echo "Pass";
}else{
echo "Failed";
}
?>
Output:
First Class
I hope this tutorial help for you.
Related Post