Re: [PHP] Re: speaking of control structures...

2009-05-07 Thread Tom Worster
On 5/6/09 4:02 PM, Al n...@ridersite.org wrote:

 Here's the way I handle validating user form inputs. Each function validates
 several things and throws an error with the message stating what's wrong.
 
   try
  {
  checkEmailAddr($userSubmitedDataArray[EMAIL_ADDR_FIELD]);
  checkPhoneDigits($userSubmitedDataArray[PHONE_NUM_FIELD],
 'phone');
  checkNotes($userSubmitedDataArray, $sizesArray);
  if(!empty($userSubmitedDataArray[CELLPHONE_NUM_FIELD]))
  {
 checkPhoneDigits($userSubmitedDataArray[CELLPHONE_NUM_FIELD],
 'cell');
  checkCellCarrier($userSubmitedDataArray['carrier']);
  }
  }
 
  catch (Exception $e)
  {
  $userErrorMsg = $e-getMessage(); //Message text in check
 function
  }
 
 A typical function looks like this:
 
 function checkEmailAddr($emailAddr)
 {
  if(empty($emailAddr))
  {
  throw new Exception(No email address provided);
  }
 
  if(!preg_match(%...@%, $emailAddr))
  {
  throw new Exception(Email address missing mailbox name.);
  }
 
  if(!filter_var($emailAddr, FILTER_VALIDATE_EMAIL))
  {
  throw new Exception(Email address error. Syntax is wrong. );
  }
  $domain = substr(strchr($emailAddr, '@'), 1);
  if(!checkdnsrr($domain))
  {
  throw new Exception(Email address warning. Specified domain
 \$domain\ appears to be invalid. Check carefully.);
  }
  return true;
 }

thanks for the example, Al. the combination of checker functions and
exceptions (as far as i understand them, the exceptions chapter of the php
manual is a little terse) so you can throw from inside the checker seems
convenient.



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



Re: [PHP] Re: speaking of control structures...

2009-05-07 Thread Al



Tom Worster wrote:

On 5/6/09 4:02 PM, Al n...@ridersite.org wrote:


Here's the way I handle validating user form inputs. Each function validates
several things and throws an error with the message stating what's wrong.

  try
 {
 checkEmailAddr($userSubmitedDataArray[EMAIL_ADDR_FIELD]);
 checkPhoneDigits($userSubmitedDataArray[PHONE_NUM_FIELD],
'phone');
 checkNotes($userSubmitedDataArray, $sizesArray);
 if(!empty($userSubmitedDataArray[CELLPHONE_NUM_FIELD]))
 {
checkPhoneDigits($userSubmitedDataArray[CELLPHONE_NUM_FIELD],
'cell');
 checkCellCarrier($userSubmitedDataArray['carrier']);
 }
 }

 catch (Exception $e)
 {
 $userErrorMsg = $e-getMessage(); //Message text in check
function
 }

A typical function looks like this:

function checkEmailAddr($emailAddr)
{
 if(empty($emailAddr))
 {
 throw new Exception(No email address provided);
 }

 if(!preg_match(%...@%, $emailAddr))
 {
 throw new Exception(Email address missing mailbox name.);
 }

 if(!filter_var($emailAddr, FILTER_VALIDATE_EMAIL))
 {
 throw new Exception(Email address error. Syntax is wrong. );
 }
 $domain = substr(strchr($emailAddr, '@'), 1);
 if(!checkdnsrr($domain))
 {
 throw new Exception(Email address warning. Specified domain
\$domain\ appears to be invalid. Check carefully.);
 }
 return true;
}


thanks for the example, Al. the combination of checker functions and
exceptions (as far as i understand them, the exceptions chapter of the php
manual is a little terse) so you can throw from inside the checker seems
convenient.




Incidentally, the throw new exception doesn't have to be in a function. it can 
be simply in your code sequence. e.g.,


if($foo != 'boo') throw new Exception(foo is not equal to boo. );

try/catch is a God sent for me. I'm big on telling the user everything that is 
wrong with their entry and what to do about it. Prior to try/catch being 
available, I'd have to have to test the return for true or a message and then 
have logic to skip over the following checks to post a message for the user.


Keep in mind, with this approach, it is most useful if you want to inform users 
about errors one at a time. If you want to get fancy, you can control the 
exception handler. See manual on this.


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



[PHP] Re: speaking of control structures...

2009-05-06 Thread Al



Tom Worster wrote:

there's a control structure i wish php had: a simple block that you can
break out of, e.g.

block {

  if ( condition )
break;

  blah...
  blah...

  if ( another condition )
break;

  blah...
  blah...

  etc...

}

the block is just like a loop except that it is executed once only.

this would be a handy structure for writing input validation code. the blah
blah fragments can be used for opening files, talking to the db,
manipulating strings, processing dates and times, etc., the conditions for
testing if the input is unacceptable.

i'm sure many of the programmers here do this kind of thing routinely and
have their own habits and solutions. i'd be curious what they are. please
let us know!


i guess i ought to go first. it's fugly but it works:

$once = true;
while ( $once ) {
  $once = false;

  stuff using break where needed ...

}

tom




Here's the way I handle validating user form inputs. Each function validates 
several things and throws an error with the message stating what's wrong.


 try
{
checkEmailAddr($userSubmitedDataArray[EMAIL_ADDR_FIELD]);
checkPhoneDigits($userSubmitedDataArray[PHONE_NUM_FIELD], 'phone');
checkNotes($userSubmitedDataArray, $sizesArray);
if(!empty($userSubmitedDataArray[CELLPHONE_NUM_FIELD]))
{
  	checkPhoneDigits($userSubmitedDataArray[CELLPHONE_NUM_FIELD], 
'cell');

checkCellCarrier($userSubmitedDataArray['carrier']);
}
}

catch (Exception $e)
{
$userErrorMsg = $e-getMessage(); //Message text in check function
}

A typical function looks like this:

function checkEmailAddr($emailAddr)
{
if(empty($emailAddr))
{
throw new Exception(No email address provided);
}

if(!preg_match(%...@%, $emailAddr))
{
throw new Exception(Email address missing mailbox name.);
}

if(!filter_var($emailAddr, FILTER_VALIDATE_EMAIL))
{
throw new Exception(Email address error. Syntax is wrong. );
}
$domain = substr(strchr($emailAddr, '@'), 1);
if(!checkdnsrr($domain))
{
throw new Exception(Email address warning. Specified domain 
\$domain\ appears to be invalid. Check carefully.);

}
return true;
}



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



[PHP] Re: speaking of control structures...

2009-05-05 Thread Maarten Schalekamp
im not sure if i understand exactly what you want. but from what ive 
gathered, did you try to make use of a FUNCTION?

?php
function add($val1,$val2) {
   return ($val1 + $val2); //returns value from where it was called.
}

function doit(){
   if() {
   bla bla bla;
   }
   else blar;
   etc...
   return; // this just returns, can also be left out.
}

$added = add(1,2);
echo $added;
doit();

?

Tom Worster f...@thefsb.org wrote in message 
news:c625f5f7.a80f%...@thefsb.org...

there's a control structure i wish php had: a simple block that you can
break out of, e.g.

block {

 if ( condition )
   break;

 blah...
 blah...

 if ( another condition )
   break;

 blah...
 blah...

 etc...

}

the block is just like a loop except that it is executed once only.

this would be a handy structure for writing input validation code. the 
blah

blah fragments can be used for opening files, talking to the db,
manipulating strings, processing dates and times, etc., the conditions for
testing if the input is unacceptable.

i'm sure many of the programmers here do this kind of thing routinely and
have their own habits and solutions. i'd be curious what they are. please
let us know!


i guess i ought to go first. it's fugly but it works:

$once = true;
while ( $once ) {
 $once = false;

 stuff using break where needed ...

}

tom



__ Information from ESET Smart Security, version of virus 
signature database 4054 (20090505) __


The message was checked by ESET Smart Security.

http://www.eset.com






__ Information from ESET Smart Security, version of virus signature 
database 4054 (20090505) __

The message was checked by ESET Smart Security.

http://www.eset.com




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



[PHP] Re: speaking of control structures...

2009-05-05 Thread Shawn McKenzie
Tom Worster wrote:
 there's a control structure i wish php had: a simple block that you can
 break out of, e.g.
 
 block {
 
   if ( condition )
 break;
 
   blah...
   blah...
 
   if ( another condition )
 break;
 
   blah...
   blah...
 
   etc...
 
 }
 
 the block is just like a loop except that it is executed once only.
 
 this would be a handy structure for writing input validation code. the blah
 blah fragments can be used for opening files, talking to the db,
 manipulating strings, processing dates and times, etc., the conditions for
 testing if the input is unacceptable.
 
 i'm sure many of the programmers here do this kind of thing routinely and
 have their own habits and solutions. i'd be curious what they are. please
 let us know!
 
 
 i guess i ought to go first. it's fugly but it works:
 
 $once = true;
 while ( $once ) {
   $once = false;
 
   stuff using break where needed ...
 
 }
 
 tom
 
 

From the PHP manual:

do {
if ($i  5) {
echo i is not big enough;
break;
}
$i *= $factor;
if ($i  $minimum_limit) {
break;
}
   echo i is ok;

/* process i */

} while (0);

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

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