Syntax switch

Syntax switch

Syntax switch is a conditional statement, but one variable can be compared not with one value, but with a few. You can  not construct complex conditions – can only be a simple comparison (equivalent statement: if ($ var == „value”) instruction).
Example

<?php

switch ($var) {
case ‚value1’:

instructions

break;
case ‚value2’:

instructions

break;
default:

instructions

}

?>

The instructions after the identifier „default” are executed if $ variable is not set values „value1” and „value2”. The instructions are processed line by line. Parser passes to the first line „case” matching variable. Then processed all the lines within the curly braces until a break statement – even if the way there are instructions „case”.
Example

<?php

$a = 3;

switch ($a) {
case 0:
case 1:
case 2:
case 3:
echo „The variable $a is less than or equal to three \ n”;
break;
case 4:
echo „The variable $a is equal to four \ n”;
break;
default:
echo „The variable $a is greater than four \ n”;
}

?>

Cookies help us deliver our services. By using our services, you agree to our use of cookies. More Info | Close