
When You write a code different actions for different conditions. Then, You can use Conditional statements in your code to do this.
If Statement:-
If Statement executes some code when one condition is true.
ex:-
<?php | |
$a = 5; | |
if ($a < 6) { | |
echo "Have a good day!"; | |
} | |
//Second example | |
if(15<19) // | |
{ | |
echo "condition is true <br>"; | |
} | |
else { | |
echo "this is true <br>"; | |
} | |
echo "this is last <br>"; | |
?> |
If …else Statement:-
If …. else Statement executes similar if statement when one condition is true and another condition is false.
ex:-
<?php | |
$a = 4; | |
if ($a < 1) { | |
echo "This is True"; //check statement if right then print if wrong then next | |
} else { | |
echo "This is False <br>"; | |
} | |
//second example | |
$a = 4; | |
if ($a < 6) { | |
echo "This is True!"; | |
} else { | |
echo "This is False"; | |
} | |
?> |
If…elseif…else Statement:-
If…elseif…else Statement executes different codes when for more than than two or multiple codes.
ex:-
<?php | |
//elseif statement | |
if(5>7) | |
{ | |
echo "this is wrong"; //check statement if right then print if wrong then next check | |
} | |
elseif(8>9) | |
{ | |
echo "this is again wrong"; | |
} | |
elseif(9>12) | |
{ | |
echo "this is too wrong"; | |
} | |
else | |
echo "this is last"; | |
echo "<br>"; | |
//second example | |
$price = 20; | |
if ($price < 10) | |
{ | |
echo "this is not right "; | |
} | |
elseif ($price < 20) | |
{ | |
echo "this is again not right"; | |
} | |
elseif ($price < 20) | |
{ | |
echo "this is too not right"; | |
} | |
else | |
echo "this is end"; | |
?> |
Switch Statement:-
Switch Statement executes one of many Statement when true.
ex:-
<?php | |
$value = 20; | |
switch ($value) | |
{ | |
case 10: | |
echo "this is first"; | |
break; | |
case 20: | |
echo "this is second"; | |
break; | |
case 30: | |
echo "this is third"; | |
break; | |
default: | |
echo "this is final"; | |
} | |
echo "<br>"; | |
$value = 10; | |
switch ($value) | |
{ | |
case 11: | |
echo "Monday <br>"; | |
break; | |
case 12: | |
echo "Tuesday <br>"; | |
break; | |
case 10: | |
echo "Sunday <br>"; | |
break; | |
default: | |
echo "End day <br>"; | |
} | |
$value = "Saturday"; | |
switch ($value) | |
{ | |
case "Monday": | |
echo "Office <br>"; | |
// break; | |
case "Tuesday": | |
echo "Office <br>"; | |
// break; | |
case "Saturday": | |
echo "Party night <br>"; | |
// break; | |
default: | |
echo "Party day <br>"; | |
} | |
?> |
If and nested if:-
If and nested if multiple code executes when true total statement print and when wrong then wrong statement print.
ex:-
<?php | |
// (if and nested if statement example) | |
if (30>2): //when true total echo print and when wrong then wrong statement print | |
echo "this is true <br>"; | |
endif; | |
echo "this is wrong <br>"; | |
if (50>55): | |
echo "this is true <br>"; | |
endif; | |
echo "this is wrong"; | |
echo "<br>"; | |
echo "<br>"; | |
echo "<br>"; | |
// second method type | |
if(10<12) | |
{ | |
echo "this is right <br>"; | |
if (22>21) | |
{ | |
echo "this is right too <br>"; | |
} | |
// echo "this is wrong too"; | |
if (45<40) | |
{ | |
echo "this is not right <br>"; | |
} | |
if(44>30) | |
{ | |
echo "this is wrong <br>"; | |
} | |
} | |
echo "this is end"; | |
?> |
If… else… nested…if:-
<?php | |
//if else | |
if(12>14){ | |
echo "this is right <br>"; | |
} | |
else | |
{ | |
echo "this is too right <br>"; | |
} | |
echo "this is wrong <br>"; | |
//if else nested if | |
if(15>14){ | |
echo "this is start <br>"; | |
if(17>18){ | |
echo "this is first <br>"; | |
} | |
else { | |
echo "this is first else <br>"; | |
} | |
} | |
else{ | |
echo "this is second else <br>"; | |
} | |
//if else nested if | |
if(15>16){ | |
echo "this is start <br>"; | |
if(17>18){ | |
echo "this is first <br>"; | |
} | |
else { | |
echo "this is first else <br>"; | |
} | |
} | |
else{ | |
echo "this is second else <br>"; | |
} | |
?> |




I’m a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I am working at Cotocus. I blog tech insights at DevOps School, travel stories at Holiday Landmark, stock market tips at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at I reviewed , and SEO strategies at Wizbrand.
Please find my social handles as below;
Rajesh Kumar Personal Website
Rajesh Kumar at YOUTUBE
Rajesh Kumar at INSTAGRAM
Rajesh Kumar at X
Rajesh Kumar at FACEBOOK
Rajesh Kumar at LINKEDIN
Rajesh Kumar at PINTEREST
Rajesh Kumar at QUORA
Rajesh Kumar at WIZBRAND