July 26, 2017

Core PHP: The Switch Statement (5.14)

Switch


Failing to specify the break statement causes PHP to continue to executing the statements that follow the case, until it finds a break. You can use this behavior if you need to arrive at the same output for more than one case.
$day = 'Wed';

switch ($day) {
  case 'Mon':
    echo 'First day of the week';
    break;
  case 'Tue':
  case 'Wed':
  case 'Thu':
    echo 'Working day';
    break;
  case 'Fri':
    echo 'Friday!';
    break;
  default:
    echo 'Weekend!';
}

//Outputs "Working day"

The example above will have the same output if $day equals 'Tue', 'Wed', or 'Thu'.

No comments:

Post a Comment