note 28778 modified in control-structures.switch by vrana

From: Date: Tue, 17 Aug 2004 13:54:37 +0000
Subject: note 28778 modified in control-structures.switch by vrana
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-79988@lists.php.net to get a copy of this message
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. --was-- 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: 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. http://php.net/manual/en/control-structures.switch.php

« previous php.notes (#79988) next »