On Tue, 2009-05-05 at 13:56 -0400, Tom Worster wrote:
> there's a control structure i wish php had: a simple block that you can
> break out of, e.g.
>
> block {
>
> if ( condition )
> break;
>
> blah...
> blah...
>
> if ( another condition )
> break;
>
> blah...
> blah...
>
> etc...
>
> }
>
> the block is just like a loop except that it is executed once only.
>
> this would be a handy structure for writing input validation code. the blah
> blah fragments can be used for opening files, talking to the db,
> manipulating strings, processing dates and times, etc., the conditions for
> testing if the input is unacceptable.
>
> i'm sure many of the programmers here do this kind of thing routinely and
> have their own habits and solutions. i'd be curious what they are. please
> let us know!
>
>
> i guess i ought to go first. it's fugly but it works:
>
> $once = true;
> while ( $once ) {
> $once = false;
>
> stuff using break where needed ...
>
> }
You can do it simpler:
<?php
while( $condition )
{
// stuff using break where needed.
// final break
break;
}
?>
But PHP 5.3 introduces goto:
<?php
header:
if( $something ) ...
goto body;
body:
if( $soemthingElse ) ...
goto footer;
if( $seomthingerElse ) ...
goto footer;
footer:
// blah blah blah
goto done;
done;
?>
Sometimes goto can make your code more succinct AND readable-- Dijkstra
be damned on this point-- All hail Knuth!
And no, I'm not saying this particular example is a good example of when
goto is useful... but it does illustrate not having to contort variables
and logic into a nightmare of complexity and unintuitiveness to satisfy
"Structured Programming" paradigms.
Cheers,
Rob.
--
http://www.interjinn.com
Application and Templating Framework for PHP
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php