The if...else statement is used to execute some code only if a specified condition is true.
if(condition){
code to be executed if condition is true;
}else{
code to be executed if condition is false;
}
Here is the code of if...else statement.
Code | |||||||
---|---|---|---|---|---|---|---|
<?php // PHP if...else statement $x=20; $y="10"; if($x > $y){ echo "$x is greater than $y"; }else{ echo "$y is less than $x"; } ?> |
Output | |||||||
---|---|---|---|---|---|---|---|
20 is greater than 10 |
Code | |||||||
---|---|---|---|---|---|---|---|
<?php //ifelse1 $a = 10; $b = 9; if(a <= b){ echo "$a is less than equal to $b"; }else{ echo "$a is greater than equal to $b"; } ?> |
Type Url - https://localhost/php/ifelse1.php
Code | |||||||
---|---|---|---|---|---|---|---|
<?php //elsegreater $a = 10; $b = 20; if(a > b){ echo "$a is greater than $b"; }else{ echo "$a is less than $b"; } ?> |
Type Url - https://localhost/php/elsegreater.php
Code | |||||||
---|---|---|---|---|---|---|---|
<?php //elsegreater1 $a = 10; $b = 11; if(a >= b){ echo "$a is greater than equal to $b"; }else{ echo "$a is less than equal to $b"; } ?> |
Type Url - https://localhost/php/elsegreater1.php
Code | |||||||
---|---|---|---|---|---|---|---|
<?php //elseequal $a = 11; $b = 10; if(a == b){ echo "$a is equal to $b"; }else{ echo "$a is not equal to $b"; } ?> |
Type Url - https://localhost/php/elseequal.php
Code | |||||||
---|---|---|---|---|---|---|---|
<?php //elsenot $a = 10; $b = 10; if(a != b){ echo "$a is not equal to $b"; }else{ echo "$a is equal to $b"; } ?> |
Type Url - https://localhost/php/elsenot.php