Main Content

Improving the structure of your early PHP programs

Archive - Originally posted on "The Horse's Mouth" - 2009-05-25 19:06:28 - Graham Ellis

When you first coded in PHP, you probably wrote a different script to handle each form in a series - it's the natural way when you're early in the learning process, but it can lead to repeated code that's hard to follow, and some really horrid complicated conditionals.

On Saturday and Sunday, I demonstrated the restructuring of a "classic" series of scripts into a more robust, more efficient, and more maintainable piece of code - all in a single application (and in a single file in this case). Duplicate code was removed, variables in the session simplified, and by using a "current page number" scheme, a whole host of complex conditionals got shrunk out of existence. I can't publish the final code (as the code on which it was based, thus my derivative, isn't my copyright) but I can talk about it ... and I can point you to a sample of a similar structure - its here

Plan out your application. If you want a system into which people have to log in, or you don't want then to start halfway through, you'll write a single piece of code. Start off your design with a state diagram, showing (in circles) each of the pages that the user may see, then linking them with lines which represent the actual program chunks (your PHP isn't the stuff in the circles - it's the lines BETWEEN them!)


Write the code all in a single top level file - i.e. at a single URL. The code will have a $_SESSION variable which holds the current page so that it knows what to run, and each time the page is called up the actions performed will be:
• a) Validate form inputs / finish up from previous page
• b) Prepare for next page
• c) Complete the template for the next page, send it out

With a small demonstration such as the one we did over the weekend, all the code and even the template can go in a single file - however, as the application grows and as you want to reuse code in other applications you'll want to split it into four files / logical groups:
• 1. The Template for the response page (easily edited with Dreamweaver or similar)
• 2. Web helper routines to help with generation of sticky forms, handling user inputs which include awkward characters such as ' " and <, etc
• 3. "Business" or program logic which relates to the application and calculations and validations that it needs
• 4. Top level code - i.e. the two switch statements that control the whole program's operation.

With a still larger system (let's say you get up to 20 or 30 pages!), you can then load business logic from multiple files, using a 'just in time' technique which will be efficient, rather that loading all the code for every page each time.


Although a 'normal' computer program runs from top to bottom, a web application written as I have described above will run a number of times ... and the way it is structure means that some of the code that's further down the file will be run before code that's higher up, but on a later page. The note in green on the board says "Confusing" - which it might be for the first half hour. Once you're used to it, it's great.