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

Conteúdo do Curso

Introduction to PHP

Introduction to PHP

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

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 a break, 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:

php

main

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:

php

main

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.

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.

Tudo estava claro?

Seção 3. Capítulo 4
toggle bottom row

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 a break, 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:

php

main

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:

php

main

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.

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.

Tudo estava claro?

Seção 3. Capítulo 4
toggle bottom row

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 a break, 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:

php

main

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:

php

main

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.

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.

Tudo estava claro?

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 a break, 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:

php

main

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:

php

main

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.

Seção 3. Capítulo 4
Mude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
We're sorry to hear that something went wrong. What happened?
some-alt