RE: [PHP] checking
| From: | Matt Schroebel | Date: | Fri, 28 Jun 2002 13:44:45 +0000 |
| Subject: | RE: [PHP] checking | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-104318@lists.php.net to get a copy of this message | ||
> if ($lastname="") {
> $insert="no"
> }
Even though you should use either empty() or isset(), you'd have a parse error if you wrote
that as:
if (""=$lastname) {
$insert="no"
}
It takes a little time to learn to write the test 'backwards' but if you do the php parser
will help and catch your single = typo when you really wanted a compare ==. I call that defensive
programming.
Another thing I do in my defensive programming tool kit is to use fuzzy compares to trap errors. For
instance, say you're writing an application to maintain a price list for products, and are
validating that the price. You could write the validation to check for == 0, and output an error in
that case. But it's more defensive to check for <= 0, that way if data gets corrupted
somehow, you'll find out about it sooner rather than later.
I also write my applications to validate the data on retrieval from the database, as well as when
the user to requesting update. If you do that, any corruption of the data will be seen by the user
even when inquiring on items. So again problems are found sooner rather than later.