note 63366 added to control-structures.switch

From: Date: Tue, 21 Mar 2006 13:16:11 +0000
Subject: note 63366 added to control-structures.switch
Groups: php.notes 
Request: Send a blank email to php-notes+get-106149@lists.php.net to get a copy of this message
Use of break after return. It may be obvious, but many user samples insist exposing unreachable code. If you use return, subsequent code will not be executed. So break statements are not needed. $a=1; echo foo($a); function foo($a){ switch($a){ case 1: return 'Desperate value returned!'; $retval='One'; break; case 2: $retval='Two'; break; default: $retval='Any other value'; } return $retval; } foo return 'Desperate...'. The code execution does not reach the lines following the return, so break; is superfluous. The code: function foo($a){ switch($a){ case 1: return 'One'; case 2: $retval='Two'; break; default: $retval='Any other value'; } return $retval; } will do the work as well. Final and helping code should be a bit light and cleaner... ---- Server IP: 66.163.161.117 Probable Submitter: 80.36.106.52 ---- X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER autolearn=no version=2.64 ---- Manual Page -- http://www.php.net/manual/en/control-structures.switch.php Edit -- http://master.php.net/note/edit/63366 Del: integrated -- http://master.php.net/note/delete/63366/integrated Del: useless -- http://master.php.net/note/delete/63366/useless Del: bad code -- http://master.php.net/note/delete/63366/bad+code Del: spam -- http://master.php.net/note/delete/63366/spam Del: non-english -- http://master.php.net/note/delete/63366/non-english Del: in docs -- http://master.php.net/note/delete/63366/in+docs Del: other reasons-- http://master.php.net/note/delete/63366 Reject -- http://master.php.net/note/reject/63366 Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#106149) next »