note 120342 added to control-structures.switch

From: Date: Fri, 16 Dec 2016 05:49:55 +0000
Subject: note 120342 added to control-structures.switch
Groups: php.notes 
Request: Send a blank email to php-notes+get-208606@lists.php.net to get a copy of this message
One thing that isn't explicitly stated about switch is that, whether blank or not, if you don't include break, it will always proceed to the next case. <?php foreach (array(0, 1, 2, 3, 4) as $foo) { switch($foo) { case 1: echo 'a'; case 0: echo '0'; break; case 2: echo 'b'; case 3: echo 'c'; case 4: echo 'd'; default: echo '<BR>'; } } Will return: abcd 0bcd cd d This can be useful in certain cases, when certain values should be supersets of other values, and can be cleaner than the corresponding if statement. You can use this to call functions and effectively have multiple default cases. <?php switch($install_step) { case MY_CONST_DOWNLOAD: download(); case MY_CONST_INSTALL: install(); case MY_CONST_CLEANUP: cleanup(); case MY_CONST_EMAIL_USER: emailUser(); } ---- Server IP: 72.52.91.14 Probable Submitter: 100.35.182.122 ---- Manual Page -- http://php.net/manual/en/control-structures.switch.php Edit -- https://master.php.net/note/edit/120342 Del: integrated -- https://master.php.net/note/delete/120342/integrated Del: useless -- https://master.php.net/note/delete/120342/useless Del: bad code -- https://master.php.net/note/delete/120342/bad+code Del: spam -- https://master.php.net/note/delete/120342/spam Del: non-english -- https://master.php.net/note/delete/120342/non-english Del: in docs -- https://master.php.net/note/delete/120342/in+docs Del: other reasons-- https://master.php.net/note/delete/120342 Reject -- https://master.php.net/note/reject/120342 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#208606) next »