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

Kursinnhold

Introduction to PHP

Introduction to PHP

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

book
Else 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:

php

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"; } ?>
Oppgave

Swipe to start coding

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

Løsning

Switch to desktopBytt til skrivebordet for virkelighetspraksisFortsett der du er med et av alternativene nedenfor
Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 3. Kapittel 3
toggle bottom row

book
Else 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:

php

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"; } ?>
Oppgave

Swipe to start coding

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

Løsning

Switch to desktopBytt til skrivebordet for virkelighetspraksisFortsett der du er med et av alternativene nedenfor
Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 3. Kapittel 3
Switch to desktopBytt til skrivebordet for virkelighetspraksisFortsett der du er med et av alternativene nedenfor
Vi beklager at noe gikk galt. Hva skjedde?
some-alt