Contenido del Curso
Introduction to PHP
Introduction to PHP
Else If Statement
Now, let's explore a scenario where multiple conditions come into play:
main
<?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:
main
<?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:
main
<?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"; } ?>
Swipe to show code editor
Fill in the blanks in the code to check temperature and weather conditions using an if ... else if ... else
structure.
¡Gracias por tus comentarios!
Else If Statement
Now, let's explore a scenario where multiple conditions come into play:
main
<?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:
main
<?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:
main
<?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"; } ?>
Swipe to show code editor
Fill in the blanks in the code to check temperature and weather conditions using an if ... else if ... else
structure.
¡Gracias por tus comentarios!