If-Else and Elseif Statement

Share this post on:

One of the fundamental constructs in PHP is the if-else statement, which allows developers to create conditional statements that execute different blocks of code based on specific conditions.

In this blog, we will discuss the basics of the if-else and elseif statements in PHP and provide examples to illustrate their usage.

The If-Else Statement

The if-else statement is used to execute different blocks of code depending on whether a certain condition is true or false. The basic syntax for the if-else statement in PHP is as follows:

if (condition) {
   // execute this block of code if the condition is true
} else {
   // execute this block of code if the condition is false
}

In the above example, if the condition is true, the code within the first block will be executed. Otherwise, the code within the else block will be executed.

Let’s look at an example:

$score = 85;

if ($score >= 60) {
   echo "Congratulations! You passed the exam!";
} else {
   echo "Sorry, you did not pass the exam.";
}

In this example, if the value of the $score variable is greater than or equal to 60, the first block of code will be executed, and following output displayed.

Congratulations! You passed the exam!

Otherwise, the second block of code will be executed, and following output displayed.

Sorry, you did not pass the exam.

The Elseif Statement

The “elseif ” statement is used for check multiple conditions. The elseif statement is an extension of the if statement and allows for more complex conditional logic.

The basic syntax for the elseif statement in PHP is as follows:

if (condition1) {
   // execute this block of code if condition1 is true
} elseif (condition2) {
   // execute this block of code if condition1 is false and condition2 is true
} else {
   // execute this block of code if both condition1 and condition2 are false
}

Let’s look at an example:

$score = 85;

if ($score >= 90) {
   echo "Congratulations! You earned an A!";
} elseif ($score >= 80) {
   echo "Good job! You earned a B.";
} elseif ($score >= 70) {
   echo "Not bad! You earned a C.";
} elseif ($score >= 60) {
   echo "You passed, but you need to work harder.";
} else {
   echo "Sorry, you did not pass the exam.";
}

In this example, there are multiple conditions to check, and the elseif statement allows us to execute different blocks of code depending on the value of the $score variable.

The code within the first block will be executed if $score is greater than or equal to 90, the second block will be executed if $score is greater than or equal to 80 but less than 90, and so on.

Conclusion

The if-else and elseif statements are fundamental constructs in PHP that allow developers to create conditional statements that execute different blocks of code based on specific conditions.

By understanding the basics of these statements and how to use them, developers can write more complex and powerful code to create dynamic web pages and web applications.

Share this post on:

Leave a Reply