Re: check a variable after EACH function

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Tue, Jun 30, 2009 at 06:31:54PM -0500, Flint Million wrote:

> This might seem silly but here's what I'm trying to do
> 
> Suppose I have some kind of check variable - say for example
> $abort_now. Or it could be a function. Something to be evaluated to a
> value.
> 
> I want to execute a block of statements, but after EACH statement
> executes, check the value of $abort_now and if it is true, break; out
> of the block.
> 
> Here's an example
> 
> do {
>   do_something();
>   do_something_else();
>   do_another_thing();
>   do_yet_another_thing();
>   and_keep_doing_things();
> } while ($abort_now != 1);
> 
> What I want to happen is for each statement to execute, and keep
> looping around, until the $abort_now variable is set to 1. Now,
> suppose any one of the statements in that block may cause $abort_now
> to become 1. If that happens, I want the block to stop executing
> immediately and not continue executing further statements.
> 
> For example, do_another_thing() causes $abort_now to equal 1. I do not
> want do_yet_another_thing or keep doing things to execute. I want the
> loop to stop right there.
> 
> The only way I can think of doing it is to insert a check after each
> statement:
> 
> do {
>   do_something();
>   if ($abort_now == 1) { break; }
>   do_something_else();
>   if ($abort_now == 1) { break; }
>   do_another_thing();
>   if ($abort_now == 1) { break; }
>   do_yet_another_thing();
>   if ($abort_now == 1) { break; }
>   and_keep_doing_things();
>   if ($abort_now == 1) { break; }
> } while (TRUE);
> 
> This might work for 2 or 3 statements but imagine a block of say 15
> statements. Having a check after each one would look ugly, and cause
> trouble if the condition needed to be changed or if I instead decided
> to check it, say, against a function.
> 
> So is this possible to do with built in code? or am I stuck with
> having to put a check after each statement in?

Aside from Shawn's exception method, you're relatively limited in how
you do this. All the other methods amount to essentially what you're
doing here. There's no other loop structure in PHP that will do it any
better than what you've devised.

FWIW, I've had to do this exact thing with regard to form validation,
except I'm not looping. Check each condition, and if it fails, never
mind validating the rest of the fields.

If you're concerned about the possibility of having to replace the
$abort_now variable with a function later, you have two choices. First,
use an editor which does search-and-replace efficiently. Second, set up
a function which is called wherever you have $abort_now currently. For
the moment, have that function simply check the $abort_now variable. In
the future, you could have it do something else, but not have to change
your existing code.

Paul

-- 
Paul M. Foster

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux