Course Banner


PHP If Else

Like other programming languages PHP also has control statements like if.. else and else if statement. Though it’s simple to understand but it’s one of the building blocks of any programming language and for PHP also.

Example:

< ?php
  1. $num1=10;
  2. $num2=30;
  3. If($num1>$num2){
  4.       echo$num1 ."is greater than - ".$num2;
  5. }
  6. else{
  7.       echo$num2 . " is greater than - ".$num1;
  8. }
  9. ?>
  10. }
  11. else{
  12.       echo$num3 . " is greater than - ".$num1. " and ".$num2;
  13. }
  14. ?>

And here is output

The above statement has two conditions if and else. What is there are more than two numbers. If there are more than two numbers then there will be else if condition also.

Example:

< ?php
  1. $num1=10;
  2. $num2=30;
  3. $num3=50;
  4. if($num1>$num2<span class='echo'>&&$num1>$num3){
  5.       echo$num1 . " I s greater than - ".$num2. " and ".$num3;
  6. }
  7. elseif($num2>$num1<span class='echo'>&&$num2>$num3){
  8.       echo$num2 . " is greater than - ".$num1. " and ".$num3;
  9. }
  10. else{
  11.       echo$num3 . " is greater than - ".$num1. " and ".$num2;
  12. }
  13. ?>

Output: