PHP switch statement

A switch statement is used when you have multiple possibilities for the if statement.

Figure - Flowchart of switch Statement:

php-switch

Example of a PHP Program to Demonstrate Switch Statement

Example:
<?php
$myFavColor='red';
switch ($myFavColor)
{
case 'pink':
  echo 'My favorite car color is pink!';
  break;
case 'red':
  echo 'My favorite car color is red!';
  break;
case 'orange':
  echo 'My favorite car color is orange!';
  break;
default:
  echo 'My favorite car color is not pink, red, or orange!';
}
?>  

Scroll Back to Top