PHP if..Else..Elseif
PHP conditional statements are used to test the conditions. It supports different types of conditional statement. Following are the types of conditional statement
- if statement
- ..else statement
- ..elseif....else statement
- switch statement
PHP if Statement PHP if statement is used to execute the code block if the one condition is true.
Syntax:
if (condition) { Php code to be executed if condition is true; }Example:
<!DOCTYPE html> <html> <head> <title>PHP Tutorial</title> </head> <body> <?php $nos=5; if($nos==5){ echo "Nos is Equall to: 5"; } ?> </body> </html>Output
Nos is Equall to 5PHP if… else Statement PHP if- else statement is used to execute the condition whether it is true of false. Syntax:
if (condition) { php code will be executed if condition is true; } else { phpcode will be executed if condition is false; }Example
<!DOCTYPE html> <html> <head> <title>PHP Tutorial</title> </head> <body> <?php $age=18; if($age==17){ echo "Congratulation! You are eligible for vote"; } else{ echo "Sorry! You are not eligible for vote"; } ?> </body> </html>Output
Sorry! You are not eligible for votePHP if..elseif ..else Statement PHP if- elseif-else statement is used to execute one or more condition whether it is true of false.
Syntax:
if (condition) { php code wil be executed if this condition is true; } elseif (condition) { php code wil be executed if this condition is true; } else { php code wil be executed if all conditions are false; }Example:
<!DOCTYPE html> <html> <head> <title>PHP Tutorial</title> </head> <body> <?php $day=4; if($day==1){ echo "Today is Sunday"; } elseif($day==2){ echo "Today is Monday"; } elseif($day==3){ echo "Today is Tuesday"; } elseif($day==4){ echo "Today is Wednesday"; } elseif($day==5){ echo "Today is Thursday"; } elseif($day==6){ echo "Today is Friday"; } elseif($day==7){ echo "Today is Saturday"; } else{ echo"Invalid Day !"; } ?> </body> </html>Output
Today is Wednesday