note 42455 deleted from control-structures.break by nlopess

From: Date: Mon, 17 May 2004 18:39:44 +0000
Subject: note 42455 deleted from control-structures.break by nlopess
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-69727@lists.php.net to get a copy of this message
Note Submitter: webmaster@daersys.net ---- In reference to "2et at free dot fr"s tip: Don't you think it's a bit of an overkill to use a while-loop just for a more convenient way of coding? I think a better way of keeping the number of if/elseif/else's down is to use functions instead: <?php function foo() { $query = "SELECT myAttr FROM myTable"; $db = DB::connect($dbUrl); if (DB::isError($db)) { // Level 2 indenting. echo "Error: Connection failed.<br/>"; // Exit sequence. return; /* * Note the difference: a return Statement will do just what the break in the loop would: it will make sure the rest of the function's code is not executed */ } // Back to level 1. $dbResult = $db->query($query); if (DB::isError($dbResult)) { // Level 2 indenting. echo "Error: Query failed.<br/>"; // Exit sequence. // Again, we're using a return statement here return; } // Back to lvl1 // do other stuff } // Call the function we just wrote foo(); ?> I think this provieds a more flexible way of coding (you can, for instance, make the function return false on error and thus influence the rest of the script). Performance-wise there won't be much of a difference I suppose though (seeing as there's only one iteration of the while(1) loop as far as I can see). No offense meant :) - Daerion

« previous php.notes (#69727) next »