format

PHP Logo

Don't Validate And Format In A Single Function

30th June 2018 - 5 minutes read time

I wanted to impart a piece of advice to do with validation and formatting of user input, although I've never seen anyone suggest it. I guess it would come under the single responsibility principle so it might seem obvious to some people. There can be reasons why this might at least seem like a good idea at the time.

Essentially, if you want to validate that something is correct, don't format it at the same time. These two actions should be done in separate functions or even classes. I hope to demonstrate that using a single function validate and format anything is a bad idea. I'll mainly be using PHP to demonstrate this, but the principle should be pretty much the same in any language.

Take the following function called isValid(). This is an arbitrary and simple example but shows validation and formatting in use in a single function.

Apache HTTP Logo

Apache Log File Into MySQL Table

20th April 2009 - 4 minutes read time

Apache can be set up to log all sorts of information. As of Apache 2.2 the basic log file format that a fresh install of Apache will produce will have the following format:

%h %l %u %t "%r" %>s %b

Which doesn't mean a lot to the uninitiated, so here is a short explanation of each.