PHP If Else PHP

PHP If Else  

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

  1. if(condition){  
  2. //code to be executed  
  3. }  

Flowchart

php if statement flowchart

Example

  1. <?php  
  2. $num=12;  
  3. if($num<100){  
  4. echo "$num is less than 100";  
  5. }  
  6. ?>  

Output:

12 is less than 100

PHP If-else Statement

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

Syntax

  1. if(condition){  
  2. //code to be executed if true  
  3. }else{  
  4. //code to be executed if false  
  5. }  

Flowchart

php if-else statement flowchart

Example

  1. <?php  
  2. $num=12;  
  3. if($num%2==0){  
  4. echo "$num is even number";  
  5. }else{  
  6. echo "$num is odd number";  
  7. }  
  8. ?>  

Output:

12 is even number

 

Download free E-book of PHP


#askProgrammers
Learn Programming for Free


Join Programmers Community on Telegram


Talk with Experienced Programmers


Just drop a message, we will solve your queries