PHP If else

PHP if else Conditional Statement use in my website

In if else Conditional Statement used to check the conditional statement or test condition. we can perform a different action for different condition  There are a various way to use if statement
php if else

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

If Statement:-

we have the following conditional statements: if statement executes some code if one condition is true

Finally Syntax like as:

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

Flowchart:

Finally, Example –

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

Finally Output:

22 is less than 100

 

If-else Statement:

PHP if-else Statement is executed different operations based on the different conditions whether condition True or False

If-else statement syntax-

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

Flowchart-

Example:-

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

Output:- 

22 is even number

The if…elseif….else Statement

The if….elseif…else statement executes different codes for more than two conditions.

Seems Like Syntax-

if (condition) {
    code to be executed if this condition is true;
} elseif (condition) {
    code to be executed if this condition is true;
} else {
    code to be executed if all conditions are false;
}

Another Example-

<?php
$t = date("H");

if ($t < "10") {
    echo "Have a good morning!";
} elseif ($t < "20") {
    echo "Have a good day!";
} else {
    echo "Have a good night!";
}
?>

Hence Output:-

 "Have a good morning!" if the current time is less than 10, and "Have a good day!" if the current time is less than 20.
 Otherwise it will output "Have a good night!":

 

Resource
PHP.net