check a variable after EACH function
| From: | Flint Million | Date: | Tue, 30 Jun 2009 23:31:54 +0000 |
| Subject: | check a variable after EACH function | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-294768@lists.php.net to get a copy of this message | ||
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