TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Advanced PHP Programming (http://www.talkphp.com/advanced-php-programming/)
-   -   Breaking out of a... not a loop. (http://www.talkphp.com/advanced-php-programming/2680-breaking-out-not-loop.html)

delayedinsanity 04-22-2008 05:07 PM

Breaking out of a... not a loop.
 
Is there any way, say if you were using curly braces to organize your code into blocks, ie;

PHP Code:

function somecall() {
$var "somevar";

    {
     if (
$then) { do(); }
     while (!
$crashed) { crash(); }
    }



can you break out of those blocks like you can break out of a loop? Dependent of course on something evaluating to true, say halfway through the block there's an if statement that checks a value and if it's true, I don't want to process the rest of the block.

This is for purely aesthetic reasons at this point, I'm trying to create a block of if statements that will look more like a switch statement than a spaghetti mess of if's and else if's.

PHP Code:

{
  if (empty(
$value)) { dothis(); break; }
  if (!
ctype_alpha($value)) { dothis(); break; }
  if (
strcasecmp($value$othervalue)) { dothis(); break; }


-m

Salathe 04-22-2008 06:22 PM

Within a function you can return at any point.
PHP Code:

function check($value)
{
  if (empty(
$value)) { dothis(); return; }
  if (!
ctype_alpha($value)) { dothis(); return; }
  if (
strcasecmp($value$othervalue)) { dothis(); return; }


As for otherwise, what's the harm in using elseif to string your checks together? In other words, why is this (bearing in mind, it's not syntactically correct PHP):
PHP Code:

{
  if (empty(
$value)) { dothis(); break; }
  if (!
ctype_alpha($value)) { dothis(); break; }
  if (
strcasecmp($value$othervalue)) { dothis(); break; }


any prettier than:
PHP Code:

if     (empty($value))                   dothis();
elseif (!
ctype_alpha($value))            dothat();
elseif (
strcasecmp($value$othervalue)) dothose(); 

If you absolutely insist in using break then you'll need to use some form of looping structure (to loop once) out of which you can break (note: not pretty!) :

PHP Code:

$foo 'car';
do {
    if (
$foo === 'aar') { echo 'aar'; break; }
    if (
$foo === 'bar') { echo 'bar'; break; }
    if (
$foo === 'car') { echo 'car'; break; }
    if (
$foo === 'dar') { echo 'dar'; break; }
    echo 
'ear';
} while (
0); 


delayedinsanity 04-22-2008 06:30 PM

Yeah, in retrospect I jumped the gun asking that question. I know break wasn't going to work, I had already tested that one. I had a huge block of else if's and I was just looking at different ways of writing it so that I could still read it later on. Turns out using curly braces to block out each specific group and using comments to label them made it easily readable on its own.

I just think too much, and usually not about useful stuff. Thanks anyways. :)
-m


All times are GMT. The time now is 02:00 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0