Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Else If Statement | Conditional Statements
Introduction to PHP
course content

Course Content

Introduction to PHP

Introduction to PHP

1. First Acquaintance
2. Variables and Data Types
3. Conditional Statements
4. Arrays
5. Loops

bookElse If Statement

Now, let's explore a scenario where multiple conditions come into play:

php

main

copy
1234567891011121314151617181920212223
<?php $day = "Wednesday"; if ($day == "Monday") { echo "Today is Monday"; } if ($day == "Tuesday") { echo "Today is Tuesday"; } if ($day == "Wednesday") { echo "Today is Wednesday"; } if ($day == "Thursday") { echo "Today is Thursday"; } if ($day == "Friday") { echo "Today is Friday"; } ?>

The drawbacks of this code are that each condition is checked separately, even after one has already been found true, leading to unnecessary checks and inefficiency. Additionally, because separate if statements are used, it's not possible to add an else condition to handle cases when none of the conditions are true. Moreover, if the value of the $day variable theoretically matches multiple conditions, the code would output multiple messages at once. To optimize this, you could use an if ... else if ... else construct, which would avoid redundant checks and allow for a default case.

Introducing else if


The else if construct provides a solution for selecting a specific code block within a series of conditions:

This pseudocode demonstrates sequential conditional checks using if, else if, and else. The program selects the first block of code whose condition evaluates to true, otherwise moves to the next else if. Let's apply this to our example:

php

main

copy
123456789101112131415
<?php $day = "Wednesday"; if ($day == "Monday") { echo "Today is Monday"; } else if ($day == "Tuesday") { echo "Today is Tuesday"; } else if ($day == "Wednesday") { echo "Today is Wednesday"; } else if ($day == "Thursday") { echo "Today is Thursday"; } else if ($day == "Friday") { echo "Today is Friday"; } ?>

Now, we've created a sequence of conditions. When at least one if condition becomes true, the chain is interrupted.

Note

This structure is useful when you only need one condition to be satisfied.

Adding else


You can also add an else statement after the condition chain. Let's modify our example:

php

main

copy
1234567891011121314151617
<?php $day = "Sunday"; if ($day == "Monday") { echo "Today is Monday"; } else if ($day == "Tuesday") { echo "Today is Tuesday"; } else if ($day == "Wednesday") { echo "Today is Wednesday"; } else if ($day == "Thursday") { echo "Today is Thursday"; } else if ($day == "Friday") { echo "Today is Friday"; } else { echo "No condition is satisfied"; } ?>

Task

Fill in the blanks in the code to check temperature and weather conditions using an if ... else if ... else structure.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 3. Chapter 3
toggle bottom row

bookElse If Statement

Now, let's explore a scenario where multiple conditions come into play:

php

main

copy
1234567891011121314151617181920212223
<?php $day = "Wednesday"; if ($day == "Monday") { echo "Today is Monday"; } if ($day == "Tuesday") { echo "Today is Tuesday"; } if ($day == "Wednesday") { echo "Today is Wednesday"; } if ($day == "Thursday") { echo "Today is Thursday"; } if ($day == "Friday") { echo "Today is Friday"; } ?>

The drawbacks of this code are that each condition is checked separately, even after one has already been found true, leading to unnecessary checks and inefficiency. Additionally, because separate if statements are used, it's not possible to add an else condition to handle cases when none of the conditions are true. Moreover, if the value of the $day variable theoretically matches multiple conditions, the code would output multiple messages at once. To optimize this, you could use an if ... else if ... else construct, which would avoid redundant checks and allow for a default case.

Introducing else if


The else if construct provides a solution for selecting a specific code block within a series of conditions:

This pseudocode demonstrates sequential conditional checks using if, else if, and else. The program selects the first block of code whose condition evaluates to true, otherwise moves to the next else if. Let's apply this to our example:

php

main

copy
123456789101112131415
<?php $day = "Wednesday"; if ($day == "Monday") { echo "Today is Monday"; } else if ($day == "Tuesday") { echo "Today is Tuesday"; } else if ($day == "Wednesday") { echo "Today is Wednesday"; } else if ($day == "Thursday") { echo "Today is Thursday"; } else if ($day == "Friday") { echo "Today is Friday"; } ?>

Now, we've created a sequence of conditions. When at least one if condition becomes true, the chain is interrupted.

Note

This structure is useful when you only need one condition to be satisfied.

Adding else


You can also add an else statement after the condition chain. Let's modify our example:

php

main

copy
1234567891011121314151617
<?php $day = "Sunday"; if ($day == "Monday") { echo "Today is Monday"; } else if ($day == "Tuesday") { echo "Today is Tuesday"; } else if ($day == "Wednesday") { echo "Today is Wednesday"; } else if ($day == "Thursday") { echo "Today is Thursday"; } else if ($day == "Friday") { echo "Today is Friday"; } else { echo "No condition is satisfied"; } ?>

Task

Fill in the blanks in the code to check temperature and weather conditions using an if ... else if ... else structure.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 3. Chapter 3
toggle bottom row

bookElse If Statement

Now, let's explore a scenario where multiple conditions come into play:

php

main

copy
1234567891011121314151617181920212223
<?php $day = "Wednesday"; if ($day == "Monday") { echo "Today is Monday"; } if ($day == "Tuesday") { echo "Today is Tuesday"; } if ($day == "Wednesday") { echo "Today is Wednesday"; } if ($day == "Thursday") { echo "Today is Thursday"; } if ($day == "Friday") { echo "Today is Friday"; } ?>

The drawbacks of this code are that each condition is checked separately, even after one has already been found true, leading to unnecessary checks and inefficiency. Additionally, because separate if statements are used, it's not possible to add an else condition to handle cases when none of the conditions are true. Moreover, if the value of the $day variable theoretically matches multiple conditions, the code would output multiple messages at once. To optimize this, you could use an if ... else if ... else construct, which would avoid redundant checks and allow for a default case.

Introducing else if


The else if construct provides a solution for selecting a specific code block within a series of conditions:

This pseudocode demonstrates sequential conditional checks using if, else if, and else. The program selects the first block of code whose condition evaluates to true, otherwise moves to the next else if. Let's apply this to our example:

php

main

copy
123456789101112131415
<?php $day = "Wednesday"; if ($day == "Monday") { echo "Today is Monday"; } else if ($day == "Tuesday") { echo "Today is Tuesday"; } else if ($day == "Wednesday") { echo "Today is Wednesday"; } else if ($day == "Thursday") { echo "Today is Thursday"; } else if ($day == "Friday") { echo "Today is Friday"; } ?>

Now, we've created a sequence of conditions. When at least one if condition becomes true, the chain is interrupted.

Note

This structure is useful when you only need one condition to be satisfied.

Adding else


You can also add an else statement after the condition chain. Let's modify our example:

php

main

copy
1234567891011121314151617
<?php $day = "Sunday"; if ($day == "Monday") { echo "Today is Monday"; } else if ($day == "Tuesday") { echo "Today is Tuesday"; } else if ($day == "Wednesday") { echo "Today is Wednesday"; } else if ($day == "Thursday") { echo "Today is Thursday"; } else if ($day == "Friday") { echo "Today is Friday"; } else { echo "No condition is satisfied"; } ?>

Task

Fill in the blanks in the code to check temperature and weather conditions using an if ... else if ... else structure.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Now, let's explore a scenario where multiple conditions come into play:

php

main

copy
1234567891011121314151617181920212223
<?php $day = "Wednesday"; if ($day == "Monday") { echo "Today is Monday"; } if ($day == "Tuesday") { echo "Today is Tuesday"; } if ($day == "Wednesday") { echo "Today is Wednesday"; } if ($day == "Thursday") { echo "Today is Thursday"; } if ($day == "Friday") { echo "Today is Friday"; } ?>

The drawbacks of this code are that each condition is checked separately, even after one has already been found true, leading to unnecessary checks and inefficiency. Additionally, because separate if statements are used, it's not possible to add an else condition to handle cases when none of the conditions are true. Moreover, if the value of the $day variable theoretically matches multiple conditions, the code would output multiple messages at once. To optimize this, you could use an if ... else if ... else construct, which would avoid redundant checks and allow for a default case.

Introducing else if


The else if construct provides a solution for selecting a specific code block within a series of conditions:

This pseudocode demonstrates sequential conditional checks using if, else if, and else. The program selects the first block of code whose condition evaluates to true, otherwise moves to the next else if. Let's apply this to our example:

php

main

copy
123456789101112131415
<?php $day = "Wednesday"; if ($day == "Monday") { echo "Today is Monday"; } else if ($day == "Tuesday") { echo "Today is Tuesday"; } else if ($day == "Wednesday") { echo "Today is Wednesday"; } else if ($day == "Thursday") { echo "Today is Thursday"; } else if ($day == "Friday") { echo "Today is Friday"; } ?>

Now, we've created a sequence of conditions. When at least one if condition becomes true, the chain is interrupted.

Note

This structure is useful when you only need one condition to be satisfied.

Adding else


You can also add an else statement after the condition chain. Let's modify our example:

php

main

copy
1234567891011121314151617
<?php $day = "Sunday"; if ($day == "Monday") { echo "Today is Monday"; } else if ($day == "Tuesday") { echo "Today is Tuesday"; } else if ($day == "Wednesday") { echo "Today is Wednesday"; } else if ($day == "Thursday") { echo "Today is Thursday"; } else if ($day == "Friday") { echo "Today is Friday"; } else { echo "No condition is satisfied"; } ?>

Task

Fill in the blanks in the code to check temperature and weather conditions using an if ... else if ... else structure.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Section 3. Chapter 3
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
some-alt