note 28778 deleted from control-structures.switch by crell
| From: | crell@php.net | Date: | Mon, 27 Jun 2022 20:37:32 +0000 |
| Subject: | note 28778 deleted from control-structures.switch by crell | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-218634@lists.php.net to get a copy of this message | ||
Note Submitter: rmunn at pobox dot com
----
In answer to njones at fredesign dot com, what you're seeing is the way the switch statement is
supposed to work. The switch statement evaluates the cases, top to bottom, until it finds the first
one that matches the value being switch()ed on. So, for example, if you had:
<?php
switch(2) {
case 1: echo "One\n"; break;
case 2: echo "Two\n"; break;
case 3: echo "Three\n"; break;
case 2: echo "Two again\n"; break;
}
?>
Only "Two" would get echoed. "Two again" would NOT get echoed, because once the
first case matches, the rest of them do NOT get evaluated. As soon as a matching case is found, the
statements starting at that case get executed until the first break, then control flows out the
bottom of the switch block.