Re: check a variable after EACH function

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

 



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?
> 
> thanks
> 
> fm

Well, since the functions can set $abort_now, why can't they just abort?
Or throw an exception?

try {
    do {
		do_something();
		do_something_else();
		do_another_thing();
		do_yet_another_thing();
		and_keep_doing_things();
	} while (true);
} catch (Exception $e) {
    //do something or nothing
}

Then in your functions instead of setting $abort_now, do:

throw new Exception('Abort! Abort! Abort!');


-- 
Thanks!
-Shawn
http://www.spidean.com

-- 
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