There are few conditional statements in PHP that is been used for putting different conditions. All the statements are as follows:
1. PHP if statement
It execute that code only if the situation is true otherwise not.
if statement Syntax:
1 2 3 | if( condition ) { if condition is true, then execute this code; } |
Example:
1 2 3 4 5 6 | <?php $t = date("H"); if ($t < "5") { echo "PHP!"; } ?> |
Result
PHP!
2. PHP if else statement
If true then execute the result, but in case condition false then output will be other coded thing.
PHP if else Syntax:
1 2 3 4 5 | if (condition) { if condition is true, then execute this code; } else { if condition is false, then execute this code; } |
Example:
1 2 3 4 5 6 7 8 | <?php $t = date("H"); if ($t < "5") { echo "PHP!"; } else { echo "Coding"; } ?> |
Result:
PHP!
3. PHP if…else if….else statement
Sometime first condition get false then to check or test another condition we use this statement.
PHP if…else if….else Syntax:
1 2 3 4 5 6 7 | if (condition) { If condition is true, then execute this code; } else if { If condition is true, then execute this code; } else { If condition is false, then execute this code; } |
Example:
1 2 3 4 5 6 7 8 9 10 | <?php $t = date("H"); if ($t < "5") { echo "PHP!"; } elseif ($t < "4") { echo "Code!"; } else { echo "Coding!"; } ?> |
Result:
1 | PHP! |