Conteúdo do Curso
Introduction to PHP
Introduction to PHP
Switch-Case Statement
The switch
statement is a powerful tool in PHP that allows you to execute one block of code from many possible options based on the value of an expression. It's particularly useful when you have a variable that can take multiple distinct values and you want to execute different code depending on that value.
Syntax
The basic syntax of a switch
statement in PHP looks like this:
- Expression is the value that you want to compare against in the various cases.
Case
value: each case checks if the expression matches this value.- The
break
keyword is used to terminate the current case and exit the switch statement. Without abreak
, the code will continue to execute the next case (fall-through). - The
default
case executes if none of the other cases match the expression. It is optional but recommended for handling unexpected values.
Let's look at a shorter example where we determine the outcome of a Rock, Paper, Scissors game based on the player's choice:
main
<?php $playerChoice = "rock"; switch ($playerChoice) { case "rock": echo "You chose rock. Rock smashes scissors!"; break; case "paper": echo "You chose paper. Paper covers rock!"; break; case "scissors": echo "You chose scissors. Scissors cuts paper!"; break; default: echo "Invalid choice. Please choose rock, paper, or scissors."; } ?>
In this example, since $playerChoice
is "rock"
, the output will be "You chose rock. Rock smashes scissors!"
. The switch
statement checks each case in order until it finds a match. Once a match is found, the corresponding code block is executed, and the break
statement prevents the execution from falling through to the next case.
Default Case
The default
case is useful for handling unexpected values. It acts as a catch-all if none of the specified cases match the expression:
main
<?php $playerChoice = "lizard"; switch ($playerChoice) { case "rock": echo "You chose rock. Rock smashes scissors!"; break; case "paper": echo "You chose paper. Paper covers rock!"; break; case "scissors": echo "You chose scissors. Scissors cuts paper!"; break; default: echo "Invalid choice. Please choose rock, paper, or scissors."; } ?>
Since "lizard"
is not a valid choice, the output will be "Invalid choice. Please choose rock, paper, or scissors"
.
The switch
statement is a cleaner and more readable alternative to multiple if
-else if
-else
statements when you have a single expression being compared against multiple values. It's especially useful for handling multiple conditions in a compact and understandable manner. Remember to use break
to prevent fall-through and to include a default
case to handle unexpected values.
Tarefa
Fill in the blanks in the code to display the appropriate message based on the value of the variable $grade
using a switch
statement.
Obrigado pelo seu feedback!
Switch-Case Statement
The switch
statement is a powerful tool in PHP that allows you to execute one block of code from many possible options based on the value of an expression. It's particularly useful when you have a variable that can take multiple distinct values and you want to execute different code depending on that value.
Syntax
The basic syntax of a switch
statement in PHP looks like this:
- Expression is the value that you want to compare against in the various cases.
Case
value: each case checks if the expression matches this value.- The
break
keyword is used to terminate the current case and exit the switch statement. Without abreak
, the code will continue to execute the next case (fall-through). - The
default
case executes if none of the other cases match the expression. It is optional but recommended for handling unexpected values.
Let's look at a shorter example where we determine the outcome of a Rock, Paper, Scissors game based on the player's choice:
main
<?php $playerChoice = "rock"; switch ($playerChoice) { case "rock": echo "You chose rock. Rock smashes scissors!"; break; case "paper": echo "You chose paper. Paper covers rock!"; break; case "scissors": echo "You chose scissors. Scissors cuts paper!"; break; default: echo "Invalid choice. Please choose rock, paper, or scissors."; } ?>
In this example, since $playerChoice
is "rock"
, the output will be "You chose rock. Rock smashes scissors!"
. The switch
statement checks each case in order until it finds a match. Once a match is found, the corresponding code block is executed, and the break
statement prevents the execution from falling through to the next case.
Default Case
The default
case is useful for handling unexpected values. It acts as a catch-all if none of the specified cases match the expression:
main
<?php $playerChoice = "lizard"; switch ($playerChoice) { case "rock": echo "You chose rock. Rock smashes scissors!"; break; case "paper": echo "You chose paper. Paper covers rock!"; break; case "scissors": echo "You chose scissors. Scissors cuts paper!"; break; default: echo "Invalid choice. Please choose rock, paper, or scissors."; } ?>
Since "lizard"
is not a valid choice, the output will be "Invalid choice. Please choose rock, paper, or scissors"
.
The switch
statement is a cleaner and more readable alternative to multiple if
-else if
-else
statements when you have a single expression being compared against multiple values. It's especially useful for handling multiple conditions in a compact and understandable manner. Remember to use break
to prevent fall-through and to include a default
case to handle unexpected values.
Tarefa
Fill in the blanks in the code to display the appropriate message based on the value of the variable $grade
using a switch
statement.
Obrigado pelo seu feedback!
Switch-Case Statement
The switch
statement is a powerful tool in PHP that allows you to execute one block of code from many possible options based on the value of an expression. It's particularly useful when you have a variable that can take multiple distinct values and you want to execute different code depending on that value.
Syntax
The basic syntax of a switch
statement in PHP looks like this:
- Expression is the value that you want to compare against in the various cases.
Case
value: each case checks if the expression matches this value.- The
break
keyword is used to terminate the current case and exit the switch statement. Without abreak
, the code will continue to execute the next case (fall-through). - The
default
case executes if none of the other cases match the expression. It is optional but recommended for handling unexpected values.
Let's look at a shorter example where we determine the outcome of a Rock, Paper, Scissors game based on the player's choice:
main
<?php $playerChoice = "rock"; switch ($playerChoice) { case "rock": echo "You chose rock. Rock smashes scissors!"; break; case "paper": echo "You chose paper. Paper covers rock!"; break; case "scissors": echo "You chose scissors. Scissors cuts paper!"; break; default: echo "Invalid choice. Please choose rock, paper, or scissors."; } ?>
In this example, since $playerChoice
is "rock"
, the output will be "You chose rock. Rock smashes scissors!"
. The switch
statement checks each case in order until it finds a match. Once a match is found, the corresponding code block is executed, and the break
statement prevents the execution from falling through to the next case.
Default Case
The default
case is useful for handling unexpected values. It acts as a catch-all if none of the specified cases match the expression:
main
<?php $playerChoice = "lizard"; switch ($playerChoice) { case "rock": echo "You chose rock. Rock smashes scissors!"; break; case "paper": echo "You chose paper. Paper covers rock!"; break; case "scissors": echo "You chose scissors. Scissors cuts paper!"; break; default: echo "Invalid choice. Please choose rock, paper, or scissors."; } ?>
Since "lizard"
is not a valid choice, the output will be "Invalid choice. Please choose rock, paper, or scissors"
.
The switch
statement is a cleaner and more readable alternative to multiple if
-else if
-else
statements when you have a single expression being compared against multiple values. It's especially useful for handling multiple conditions in a compact and understandable manner. Remember to use break
to prevent fall-through and to include a default
case to handle unexpected values.
Tarefa
Fill in the blanks in the code to display the appropriate message based on the value of the variable $grade
using a switch
statement.
Obrigado pelo seu feedback!
The switch
statement is a powerful tool in PHP that allows you to execute one block of code from many possible options based on the value of an expression. It's particularly useful when you have a variable that can take multiple distinct values and you want to execute different code depending on that value.
Syntax
The basic syntax of a switch
statement in PHP looks like this:
- Expression is the value that you want to compare against in the various cases.
Case
value: each case checks if the expression matches this value.- The
break
keyword is used to terminate the current case and exit the switch statement. Without abreak
, the code will continue to execute the next case (fall-through). - The
default
case executes if none of the other cases match the expression. It is optional but recommended for handling unexpected values.
Let's look at a shorter example where we determine the outcome of a Rock, Paper, Scissors game based on the player's choice:
main
<?php $playerChoice = "rock"; switch ($playerChoice) { case "rock": echo "You chose rock. Rock smashes scissors!"; break; case "paper": echo "You chose paper. Paper covers rock!"; break; case "scissors": echo "You chose scissors. Scissors cuts paper!"; break; default: echo "Invalid choice. Please choose rock, paper, or scissors."; } ?>
In this example, since $playerChoice
is "rock"
, the output will be "You chose rock. Rock smashes scissors!"
. The switch
statement checks each case in order until it finds a match. Once a match is found, the corresponding code block is executed, and the break
statement prevents the execution from falling through to the next case.
Default Case
The default
case is useful for handling unexpected values. It acts as a catch-all if none of the specified cases match the expression:
main
<?php $playerChoice = "lizard"; switch ($playerChoice) { case "rock": echo "You chose rock. Rock smashes scissors!"; break; case "paper": echo "You chose paper. Paper covers rock!"; break; case "scissors": echo "You chose scissors. Scissors cuts paper!"; break; default: echo "Invalid choice. Please choose rock, paper, or scissors."; } ?>
Since "lizard"
is not a valid choice, the output will be "Invalid choice. Please choose rock, paper, or scissors"
.
The switch
statement is a cleaner and more readable alternative to multiple if
-else if
-else
statements when you have a single expression being compared against multiple values. It's especially useful for handling multiple conditions in a compact and understandable manner. Remember to use break
to prevent fall-through and to include a default
case to handle unexpected values.
Tarefa
Fill in the blanks in the code to display the appropriate message based on the value of the variable $grade
using a switch
statement.