In PHP, we found two type of decision controlling instructions – if-else statements and switch statements. Here we will discuss about if-else part of PHP.
If else are the basic conditional statements that are normally used to perform different types of actions based on some of the different conditions. When we write any program/code, one has to take decisions (conditions) to perform certain types of tasks. So we need conditional statements (if, else, etc..,) to do our tasks using the code. “If” condition/statement will make the code run only if the condition is true whereas “else” is the condition which will run only if the “if” condition becomes false.
- if statement
if the condition under if block gets true then statements gets executed.
Syntax –
if (condition){ statements; statements; } |
Let’s see some real life examples…
File – code1.php
<?php $a=10; if($a==10){ echo “a is 10”; } $b=$a; // checks integer variables ‘a’ & ‘b’ equal? if($b==$a){ echo ‘<br>’; echo ‘a and b both equal’; } ?> |

File – code2.php
<?php $a=’computer’; if($a==’computer’){ echo “string a is computer”; } $b=$a; // checks string variable ‘b’ equal to string variable value ‘a’ ? if($b==’computer’){ echo ‘<br>’; echo ‘strings b same as a’; } ?> |

File – code3.php
<?php $lst=[1,2,3]; $lst2=[1,2,3]; $lst3=[1,2]; echo ‘lst is: ‘; print_r($lst); echo ‘lst2 is: ‘; print_r($lst2); echo ‘lst3 is: ‘; print_r($lst3); // checks equality of lists if($lst==$lst2){ echo ‘both lists equal!!’; } if($lst==$lst3){ echo “<br> lst & lst3 equal”; }else{ echo “<br> lst & lst3 not equal”; } ?> |

- else_if statement
When we want to handle multiple condition, then we can use else-if statements.
Syntax:
if (condition){ statements; } else if (condition){ statements; |
file – code4.php
<?php $b=20; if($b==21){ echo ‘<br>if statement executed!’; echo ‘b is 21’; } else if($b==20){ echo ‘<br> else if statement executed!<br>’; echo ‘b is 20’; } ?> |

file – code5.php
<?php $b=’microcodes.in’; if($b==’microcodes’){ echo ‘<br>if statement executed!’; echo ‘b is microcodes’; } else if($b==’microcodes.in’){ echo ‘<br> else if statement executed!<br>’; echo ‘b is microcodes.in’; } ?> |

- else statement
When if block’s condition not satisfies then else block get executed.
Syntax –
if (condition){ statements; } else{ statements; } |
file – code6.php
<?php $name=’windows11′; if($name==’windows10′){ echo ‘os is windows 10’; } else{ echo ‘alas!! os is windows 11’; } ?> |

file – code7.php
<?php $arr=[10,20,40]; echo “array is: “; print_r($arr); // we can also compare arrays inside if-block if($arr==[10,20,50]){ echo ‘if block executed!!’; } else{ echo ‘else block executed!!’; } ?> |

- nested if
It’s simply means if statement inside another If statement. When we have multiple conditions then sometime we can use nested if block.
File – code8.php
<?php $a=30; if ($a>20) { // nested-if, >=[relational operator] usage inside if-condition-block if($a>=30){ echo ‘a is greater than 20’; echo ‘<br> a >=30 ‘; } } else{ echo ‘something error!!’; } ?> |

file – code9.php
<?php $a=’microcodes’; $b=”shimanta”; if ($a==’microcodes’) { if($b==”shimanta”){ echo ‘a is microcodes’; echo ‘<br> a equals shimanta!!’; } } else{ echo ‘something error!!’; } ?> |

- nested if_else statement
When we want to handle multiple conditions then we can use nested if-else statements.
File – code10.php
<?php $opt=100; $con=’india’; $gen=’ben 10′; if($opt==(101-1)){ echo ‘<br>opt is: ‘.$opt; if($con==’bangladesh’){ echo ‘<br>country is bangladesh!’; } // nested if-else else if($con==’india’){ echo ‘<br>country is india!’; // nested if-else if($gen==’doramon’){ echo ‘<br>cartoon is doramon’; } else{ echo ‘<br>cartoon is ben 10’; } } } ?> |

- Logical Operators:Till now, we have seen if, else if, else statements using relational operators like ><>= != etc.Now we will perform logical operations over if-else statements.
- Logical AND [ && ] – if both inputs same then output will same. The Logical AND Operator returns the Boolean value True or 1 when both the operands satisfy the conditions and are true in nature. If any one of the operands does not satisfy the condition, then it will return a False or 0 value. Here we can use ‘&&’ ‘and’ operators both.
File – code11.php
<?php $g=21; if($g==21 && $g<30){ echo ‘g is 21 and less than 30’; } ?> |

file – code12.php
<?php $g=’linux’; $G=$g; echo ‘g is: ‘.$g; echo ‘<br> G is: ‘.$G; if($g==’linux’ and $G==’linux’){ echo ‘<br>g==G==linux’; } else{ echo ‘error!!’; } ?> |

- Logical OR [ || ] – if at-least one input gets true then output same. An AND gate is a logic gate having two or more inputs and a single output. An AND gate operates on logical multiplication rules. In this gate, if either of the inputs is low (0), then the output is also low. If all of the inputs are high (1), then the output will also be high.Here we can use ‘||’ ‘or’ operators both.
File – code13.php
<?php $g=21; echo ‘g is: ‘.$g; // first condition not match but 2nd does if($g==22 || $g<30){ echo ‘<br>if-block executed!’; } ?> |

file – code14.php
<?php $g=’windows’; $G=$g; echo ‘g is: ‘.$g; echo ‘<br> G is: ‘.$G; if($g==’linux’ or $G==’windows’){ echo ‘<br>windows wins!’; } else{ echo ‘error!!’; } ?> |

- Logical NOT: here simple concept input not equals output. It’s mean, if input goes ‘1’ then output ‘0’.
File – code15.php
<?php $name=’windows11′; // not equal != if($name!=’windows10′){ echo ‘os is windows 11 not windows 10’; } // works with boolean values $age=TRUE; if($age){ echo ‘<br>age set!’; } ?> |
