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:
switch (expression) {
case value1:
// Code to be executed if expression equals value1
break;
case value2:
// Code to be executed if expression equals value2
break;
// You can have any number of case statements
default:
// Code to be executed if expression doesn't match any case
}
- Expression is the value that you want to compare against in the various cases.
Casevalue: each case checks if the expression matches this value.- The
breakkeyword 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
defaultcase 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
1234567891011121314151617<?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
1234567891011121314151617<?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.
Swipe to start coding
Fill in the blanks in the code to display the appropriate message based on the value of the variable $grade using a switch statement.
Solução
Obrigado pelo seu feedback!
single
Pergunte à IA
Pergunte à IA
Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo
Can you show me a full example of a switch statement in PHP?
What happens if I forget to use the break statement in a switch?
Are there any situations where using if-else is better than switch?
Awesome!
Completion rate improved to 4.35
Switch-Case Statement
Deslize para mostrar o menu
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:
switch (expression) {
case value1:
// Code to be executed if expression equals value1
break;
case value2:
// Code to be executed if expression equals value2
break;
// You can have any number of case statements
default:
// Code to be executed if expression doesn't match any case
}
- Expression is the value that you want to compare against in the various cases.
Casevalue: each case checks if the expression matches this value.- The
breakkeyword 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
defaultcase 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
1234567891011121314151617<?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
1234567891011121314151617<?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.
Swipe to start coding
Fill in the blanks in the code to display the appropriate message based on the value of the variable $grade using a switch statement.
Solução
Obrigado pelo seu feedback!
single