⊗ppPmCdSch 97 of 447 menu

Switch-Case Construct in PHP

PHP has a special switch-case construct, which is used to select one value from a range of values. Study its syntax:

<?php switch (variable) { case 'value1': /* code that will execute if the variable has value1 */ break; case 'value2': /* code that will execute if the variable has value2 */ break; case 'value3': /* code that will execute if the variable has value3 */ break; default: /* code that will execute if it doesn't match any value */ break; } ?>

As you can see, switch-case is an alternative to multiple elseif statements. Let's look at an example. Suppose we have the following code:

<?php $lang = 'ru'; if ($lang === 'ru') { echo 'rus'; } elseif ($lang === 'en') { echo 'eng'; } elseif ($lang === 'de') { echo 'ger'; } else { echo 'language not supported'; } ?>

Let's rewrite this code using the switch-case construct:

<?php $lang = 'ru'; switch ($lang) { case 'ru': echo 'rus'; break; case 'en': echo 'eng'; break; case 'de': echo 'ger'; break; default: echo 'language not supported'; break; } ?>

The variable $num can take values 1, 2, 3 or 4. Determine which season the value of this variable falls into.

byenru