note 125903 deleted from control-structures.switch by crell
| From: | crell@php.net | Date: | Mon, 27 Jun 2022 21:55:04 +0000 |
| Subject: | note 125903 deleted from control-structures.switch by crell | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-218652@lists.php.net to get a copy of this message | ||
Note Submitter: mparsa1372 at gmail dot com
----
This is how it works: First we have a single expression n (most often a variable), that is evaluated
once. The value of the expression is then compared with the values for each case in the structure.
If there is a match, the block of code associated with that case is executed. Use break to prevent
the code from running into the next case automatically. The default statement is used if no match is
found.
<?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!";
}
?>