switch (fruit) {
case apple:
code to be executed if fruit=apple;
break;
case banana:
code to be executed if fruit=banana;
break;
...
default:
code to be executed if fruit is different from all fruits;
}
$fruit = "apple";
switch ($fruit) {
case "apple":
echo "doctor";
break;
case "banana":
echo "monkey";
break;
default:
echo "things";
}//doctor
switch ($element) {
case 0:
echo "i es igual a 0";
break;
case 1:
echo "i es igual a 1";
break;
case 2:
echo "i es igual a 2";
break;
}
<?php
switch ($i) {
case 0:
echo "i equals 0";
break;
case 1:
echo "i equals 1";
break;
case 2:
echo "i equals 2";
break;
default:
echo "i is not equal to 0, 1 or 2";
}
?>
switch ($i) {
case 0:
echo "i equals 0";
break;
case 1:
echo "i equals 1";
break;
case 2:
echo "i equals 2";
break;
default:
echo "i is not equal to 0, 1 or 2";
}
switch ($i) {
case 0:
echo "i ist gleich 0";
break;
case 1:
echo "i ist gleich 1";
break;
case 2:
echo "i ist gleich 2";
break;
}
int value = 0;
switch (value) {
case 0:
// do something
break;
case 1:
// do something else
break;
default :
// something if anything not match
}
<?php
$favcolor = "red";
switch ($favcolor) {
case "red":
echo "Your favorite color is red!";
break;
case "blue":
echo "Your favorite color is blue!";
break;
case "green":
echo "Your favorite color is green!";
break;
default:
echo "Your favorite color is neither red, blue, nor green!";
}
?>
<?php
// switch statement:
switch ($i) {
case 0:
echo "i equals 0";
break;
case 1:
echo "i equals 1";
break;
case 2:
echo "i equals 2";
break;
default:
echo "default message";
}
?>
<?php
/*
We use the switch statement to select any one of many blocks to be
executed.
Here is an example:
*/
switch (n) {
case label1:
code to be executed if n=label1;
break;
case label2:
code to be executed if n=label2;
break;
case label3:
code to be executed if n=label3;
break;
...
default:
code to be executed if n is different from all labels;
}
/*
You assign it a variable.
Here is an example of it being used
*/
$polygon = 12
switch ($polygon) {
case 12:
echo "Dodecagon";
break;
case 18:
echo "Octadecagon";
break;
case 42:
echo "Tetracontakaidigon";
break;
default:
echo "Enneacontakaihexagon";
}
?>
foreach ($array[0] as $key => $value) {
switch ($key) {
case 'Products' :
// do something
break ;
case 'customers' :
// do something
break ;
case 'Models' :
// do something
break ;
}
}
<?php
// Ce switch:
switch ($i) {
case 0:
echo "i égal 0";
break;
case 1:
echo "i égal 1";
break;
case 2:
echo "i égal 2";
break;
}
// Équivaut à:
if ($i == 0) {
echo "i égal 0";
} elseif ($i == 1) {
echo "i égal 1";
} elseif ($i == 2) {
echo "i égal 2";
}
?>
<?php
$i=250;
Switch($i)
{
Case 150:
echo "Value is 150";
Break;
Case 250:
echo "Value is 250";
Break;
Case 350:
echo "Value is 350";
Break;
Case 450:
echo "Value is 450";
Break;
Default:
echo "Enter Correct Value";
Break;
}
?>
switch ($your_variable)
{
case 1:
case 2:
echo "the value is either 1 or 2.";
break;
}