Re: [PHP] check a variable after EACH function

2009-06-30 Thread Paul M Foster
On Tue, Jun 30, 2009 at 11:21:03PM -0400, Phpster wrote:

> On Jun 30, 2009, at 10:48 PM, Paul M Foster 
> wrote:
>



>>
>> 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.
>>
>
> Isn't that a little rough on the user? Wouldn't a better user
> experience be to check all the fields and report all errors back to
> the user in one pass, rather than after each element that fails?



Well, yes. Early on I tended to do it this way, but I made my error
handling more sophisticated over time.

Actually, where I do something like this now is on some blog software I
wrote. There are numerous fields to be filled out, and if all are
filled out correctly, it updates a database and dumps some files to
disk. At each field check, I increment an error count variable if the
user screws up. And before I check each field, I check the error count.
If there are errors, then I may not update the database and dump the
files, so there's no point in checking the contents of certain fields.
For example, if the user didn't provide a title for the post, I'm not
going to go through the process of XSS checking on their post content,
since I'm not going to store it until they give me a title. I'll report
to them on the missing fields, though, and allow them to repair.

Paul
-- 
Paul M. Foster

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



Re: [PHP] check a variable after EACH function

2009-06-30 Thread Phpster





On Jun 30, 2009, at 10:48 PM, Paul M Foster   
wrote:



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.



Isn't that a little rough on the user? Wouldn't a better user  
experience be to check all the fields and report all errors back to  
the user in one pass, rather than after each element that fails?








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




Bastien

Sent from my iPod

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



Re: [PHP] check a variable after EACH function

2009-06-30 Thread Paul M Foster
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



[PHP] check a variable after EACH function

2009-06-30 Thread Flint Million
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

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