Learn If Else, Elseif & Switch Case in PHP Easily
Conditional statements or making decisions with conditions in php:
Conditional statements are used to check whether condition is satisfied or not.there are many types in this decision makers that is if statement,if else statement,if else if else statement,switch.these are help to check the conditions and execute the different blocks of a program based on condition true .this is very important in programming languages to make right decision based on conditions check.
1.if statement
2.if-else statement
3.if-elseif-else statement
4.switch statement
1.if statement:
if statement is used to check the condition.if the condition is true it goes to if block statements.otherwise it does not print anything on that if block code.
Syntax for if statement:
if(condition)
{
Statement 1;
Statement 2;
Statement 3;
}
Program for if statement:
<?php
$a=100;
$b=200;
if($a<$b)
{
echo “b is the biggest element”;
}
Output:
b is the biggest element
Program 2:
<?php
$a=100;
$b=200;
if($a>$b)
{
echo “b is biggest element”;
}
echo “this is the if program”;
?>
Output 2:
this is the if program
Explanation:
Ikkada rasina two programs observe cheste program one , condition check chesindi . condition true kabatti if block lo unna statement anedhi print ayyindi.dani kinda program 2 chuste if condition anedhi false ayyinddi kabatti if lo unna code execute avvaledhu.dani kinda unna echo statement matrame print ayyindi.
if-else statement:
Here if-else statement is used for checking the condition . if the condition is true it goes to if block statement.if condition is false then it goes to else statement.
Syntax for if -else statement:
if(condition)
{
Statement 1;
statement 2;
Statement 3;
}
else
{
Statement 4;
}
Program for if else statement:
<?php
$a=35;
if($a>=35)
{
echo ” student a is pass”;
}
else
{
echo “student a is fail”;
}
?>
Output:
student a is pass.
Program 2:
<?php
$a=52;
$b=62;
if($a>$b)
{
echo ” a is biggest number than b”;
}
else
{
echo “b is biggest number than a”;
?>
Output:
b is biggest number than a.
Explanation:
Paina unna two programs oif ifbserve cheste ichina condition true aithe if block loni statement print avuthundi.okavela condition kanaka false aithe adhi if block ni vadilesi else block lo unna statement print avuthayi.program 2 lo biggest of two numbers program rasamu.
If-else if-else statement:
If elseif program is used to check conditions.if Two conditions dalse then it goes to else block.statement it is also called as nested if condition.
Syntax for if else if else statement:
if(condition)
{
Statement 1;
Statement 2;
}
elseif(condition)
{
Statement 3;
Statement 4;
}
else
{
Statement 5;
}
Program for if else if else statement:
<?php
$age=18;
if($age>=18)
{
echo “these are eligible for vote”;
}
elseif($age<18)
{
echo “those are not eligible for vote”;
}
else
{
echo “Those are older people for vote”;
?>
output:
Those are eligible for vote.
Switch statement:
Switch statement consists many cases which matches input case value of users that execute the particular case.there are two commands one is break and another is default.
Break is used to stop the execution of the code .
Default is used when none of the code is satisfied.
Syntax for switch:
Switch(variable)
{
Case 1:
//Execute code with some operations
break;
Case 2:
// Execute code with some operations
break;
Car 4:
//Execute code with some operations
break;
default:
//Execute none of case match
}
Program for switch statement:
<?php
$a=10;
Switch($a):
case 1:
echo $a +=10;
break;
case 2:
echo $a-=20;
break;
default:
echo “all program is executed”;
}
?>
Explanation:
E program lo manam anni operations okko case lo pertukuntamu.variable a declare chesanu kada,manam e case number enter chestamo a block of program anedi print avuthundi print ayyi ayipoyaka akkada break undi kabatti a block statement print ayyaka break avuthundi.manam ichina case value edho lekapothe manaki default lo unna block of code execute ayyi akkada unna statement print avuthundim