PHP If Else

PHP If Else

PHP if else statement is used to test condition. There are various ways to use if statement in PHP.

  • if
  • if-else
  • if-else-if
  • nested if

PHP If Statement

PHP if statement is executed if condition is true.

Syntax :

if(condition)
{  
    //code to be executed  
}  

Example

<?php  
$num=12;  
if($num<100)
{  
       echo "$num is less than 100";  
}  
?>
Output : 12 is less than 100

PHP If-else Statement

PHP if-else statement is executed whether condition is true or false.

if(condition)
{  
    //code to be executed if true  
}
else
{  
    //code to be executed if false  
}  

Image Source : c-programming-simple-steps.com

Example

<?php  
$num=12;  
if($num%2==0)
{  
     echo "$num is even number";  
}else
{  
     echo "$num is odd number";  
}  
?>  
Output : 12 is even number