PHP If, Else, ElseIf

PHP If, Else, ElseIf

In PHP, conditional statements such as if, else, and elseif are used to execute different blocks of code based on certain conditions. These statements allow you to control the flow of your program by specifying which code should run depending on whether a condition evaluates to true or false.

1. The if Statement

The a if statement is used to test a condition. If the condition is true, the block of code inside the if statement is executed. If the condition is false, the code block is skipped.

Syntax:

if (condition) { // Code to be executed if condition is true }

Example:

$age = 18; if ($age >= 18) { echo "You are eligible to vote."; }

In this example, if $age is greater than or equal to 18, the message "You are eligible to vote." will be displayed.

2. The else Statement

The else statement is used in conjunction with a if statement. It provides an alternative block of code to execute when the if condition is false.

Syntax:

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

Example:

$age = 16; if ($age >= 18) { echo "You are eligible to vote."; } else { echo "You are not eligible to vote."; }

In this example, since $age is less than 18, the output will be "You are not eligible to vote."

3. The elseif Statement

The A elseif statement allows you to test multiple conditions. If the condition in the if statement is false, it will check the condition in the elseif statement. You can have multiple elseif statements in a chain. If none of the conditions are true, the else block (if provided) will be executed.

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 both conditions are false }

Example:

$age = 25; if ($age >= 18 && $age <= 34) { echo "You are a young adult."; } elseif ($age >= 35 && $age <= 64) { echo "You are an adult."; } else { echo "You are either a minor or a senior citizen."; }

In this example, since $age is 25, the message "You are a young adult." will be displayed.

4. Multiple elseif Statements

You can have as many elseif statements as needed to check different conditions.

Example:

$score = 85; if ($score >= 90) { echo "Grade A"; } elseif ($score >= 80) { echo "Grade B"; } elseif ($score >= 70) { echo "Grade C"; } elseif ($score >= 60) { echo "Grade D"; } else { echo "Grade F"; }

In this example, the score is 85, so the output will be "Grade B" because the score is greater than or equal to 80 but less than 90.

5. Using Logical Operators

You can combine multiple conditions in if, elseif, and else statements using logical operators like && (AND), || (OR), and ! (NOT).

Example with AND (&&):

$age = 20; $hasID = true; if ($age >= 18 && $hasID) { echo "You can enter."; } else { echo "You cannot enter."; }

Here, both conditions must be true for the code inside the if block to execute.

Example with OR (||):

$day = "Saturday"; if ($day == "Saturday" || $day == "Sunday") { echo "It's the weekend!"; } else { echo "It's a weekday."; }

Here, the condition will be true if it's either Saturday or Sunday.

6. Ternary Operator

PHP also offers a shorthand way to write if, elseif, and else statements using the ternary operator. The ternary operator is often used for simple conditions.

Syntax:

condition ? value_if_true : value_if_false;

Example:

$age = 17; echo ($age >= 18) ? "You are eligible to vote." : "You are not eligible to vote.";

In this example, the condition ($age >= 18) is checked. If true, "You are eligible to vote." is displayed, otherwise "You are not eligible to vote." is shown.

7. Switch Statement (Alternative)

For multiple conditions, the a switch statement can sometimes be more efficient than using multiple if, elseif, and else statements. The switch statement evaluates an expression and compares it with multiple case values.

Syntax:

switch (expression) { case value1: // Code to be executed if expression == value1; break; case value2: // Code to be executed if expression == value2; break; default: // Code to be executed if expression does not match any case; }

Example:

$day = "Monday"; switch ($day) { case "Monday": echo "Start of the work week."; break; case "Friday": echo "Almost the weekend!"; break; case "Saturday": case "Sunday": echo "It's the weekend!"; break; default: echo "It's a weekday."; }

In this example, since $day is "Monday", the output will be "Start of the work week."

Summary

  • if: Executes a block of code if the condition is true.
  • else: Executes a block of code if the condition in the if statement is false.
  • elseif: Allows you to check multiple conditions if the previous if or elseif was false.
  • Logical Operators (&&, ||, !) can be used to combine conditions.
  • The ternary operator is a shorthand for if-else and can be used for simpler conditions.
  • The switch statement is an alternative to multiple if-else when checking the value of a single expression against many possible cases.
Soeng Souy

Soeng Souy

Website that learns and reads, PHP, Framework Laravel, How to and download Admin template sample source code free.

Post a Comment

CAN FEEDBACK
close