If..else..elseif in PHP
? If, Else, and Elseif in PHP
The if, else, and elseif statements are used in PHP for conditional programming. These allow you to execute code based on certain conditions.
1. if Statement
The if statement allows you to execute a block of code if a specific condition is true.
Syntax:
if (condition) { // Code to be executed if the condition is true}Example: Simple if Statement
<?php$age = 20;if ($age >= 18) { echo "You are an adult."; // This will be executed because the condition is true}?>In this example, since $age is 20 (which is greater than or equal to 18), the message "You are an adult." will be displayed.
2. else Statement
The else statement is used to specify a block of code that should be executed if the condition in the if statement is false.
Syntax:
if (condition) { // Code to be executed if the condition is true} else { // Code to be executed if the condition is false}Example: Using if and else
<?php$age = 16;if ($age >= 18) { echo "You are an adult.";} else { echo "You are a minor."; // This will be executed because the condition is false}?>In this case, since $age is 16, which is less than 18, the message "You are a minor." will be displayed.
3. elseif Statement
The elseif statement allows you to test multiple conditions. If the first if condition is false, the program will check the elseif condition. You can have multiple elseif statements for multiple conditions.
Syntax:
if (condition1) { // Code to be executed if condition1 is true} elseif (condition2) { // Code to be executed if condition2 is true} else { // Code to be executed if neither condition1 nor condition2 is true}Example: Using if, elseif, and else
<?php$age = 22;if ($age >= 60) { echo "You are a senior citizen.";} elseif ($age >= 18) { echo "You are an adult."; // This will be executed because $age is 22, which satisfies condition2} else { echo "You are a minor.";}?>Here, the first condition checks if $age is 60 or older (false). Then, the elseif checks if $age is 18 or older (true), so the message "You are an adult." is displayed.
4. else Without elseif
You can also use just if with else if you don't need multiple conditions.
Example: if with else
<?php$number = 5;if ($number % 2 == 0) { echo "Even number.";} else { echo "Odd number."; // This will be executed because $number is odd}?>Since 5 is an odd number, the message "Odd number." is displayed.
5. Nesting if Statements
You can nest if statements inside each other for more complex conditions.
Example: Nested if Statements
<?php$age = 25;$citizenship = "USA";if ($age >= 18) { if ($citizenship == "USA") { echo "You are eligible to vote in the USA."; } else { echo "You are not eligible to vote in the USA."; }} else { echo "You are too young to vote.";}?>Here, the program first checks if the user is 18 or older, and if true, it checks the citizenship. If the citizenship is "USA," the user is eligible to vote.
6. Combining Multiple Conditions
You can combine multiple conditions using logical operators like && (AND), || (OR), and ! (NOT).
Example: Using AND and OR
<?php$age = 25;$hasID = true;if ($age >= 18 && $hasID) { echo "You can enter the club.";} else { echo "You cannot enter the club.";}?>&&(AND): Both conditions must be true.||(OR): At least one condition must be true.!(NOT): Reverses the condition.
7. Ternary Operator (Shortened if-else)
You can use a ternary operator for simple conditional checks, which is a shortened version of if-else.
Syntax:
condition ? value_if_true : value_if_false;Example: Using Ternary Operator
<?php$age = 18;echo ($age >= 18) ? "You are an adult." : "You are a minor."; // Output: You are an adult.?>This is equivalent to:
<?phpif ($age >= 18) { echo "You are an adult.";} else { echo "You are a minor.";}?>Summary of if, elseif, and else:
if: Used to check a condition, and if true, it executes the associated code block.else: Used to execute code when theifcondition is false.elseif: Used to check multiple conditions sequentially.Logical Operators: Combine multiple conditions with
&&(AND),||(OR), and!(NOT).Ternary Operator: A shorthand version of
if-elsefor simpler conditions.