Archive - Originally posted on "The Horse's Mouth" - 2007-05-10 07:46:15 - Graham Ellis
In PHP, you can write a program that looks like a Rembrandt, or one that makes the dog's dinner look tidy. It's not only the mechanism of the language you need to learn to do a good job, but how to apply it.
This week, on my public PHP course, I have a group which is very IT professional in background, and very much concerned with writing good code efficiently, with easy maintainabiliy, enhancability, and element re-use across projects. So rather that use my mores standard "here is a program to ...", I've been writing new examples with a different twist as I go.
It's easy enough to code a box on a form to echo back what was on the previous form. But remember the user may enter all sorts of special characters that you need to deal with. And remember you need to do this multiple times. And remember that you need to be able to re-present the same form if the fields aren't correctly filled. And remember you would like to reuse near-identical logic on the next page. You're looking for nicely structured code - the Rembrandt not the dinner!
• Make good use of functions.
• Have common error handling logic.
• Put everything that varies into a single specification.
• Save the common code into an include file.
• Use good error messages.
I've put the example I finished with last night up on line - the source code is here and you can run it here. There ARE things still to be done ... - the error messages are common at the moment, and it's all in one file rather than using an include (a good way to do it for initial testing, after all!)