note 42455 added to control-structures.break
| From: | webmaster at daersys dot net | Date: | Mon, 17 May 2004 15:14:45 +0000 |
| Subject: | note 42455 added to control-structures.break | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-69720@lists.php.net to get a copy of this message | ||
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
----
Manual Page -- http://www.php.net/manual/en/control-structures.break.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+42455
Delete -- http://master.php.net/manage/user-notes.php?action=delete+42455&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+42455&report=yes
Search -- http://master.php.net/manage/user-notes.php