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

Зміст курсу

Introduction to PHP

Introduction to PHP

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

book
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

copy
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:

php

main

copy
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.

Завдання
test

Swipe to show code editor

Fill in the blanks in the code to display the appropriate message based on the value of the variable $grade using a switch statement.

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 3. Розділ 4
toggle bottom row

book
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

copy
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:

php

main

copy
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.

Завдання
test

Swipe to show code editor

Fill in the blanks in the code to display the appropriate message based on the value of the variable $grade using a switch statement.

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 3. Розділ 4
Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
We're sorry to hear that something went wrong. What happened?
some-alt