Main Content

Don't repeat code - use loops or functions

Archive - Originally posted on "The Horse's Mouth" - 2005-08-21 07:29:30 - Graham Ellis

I've just answered a question about completing a whole series of radio boxes in a form, where the questioner provided sample code that had the same line, slightly modified, appearing time and time again. But if you find yourself repeating something, there must be an easier way and that way will be either a function or a loop ... the initial coding may be more complex, but when you come to expand your list of options from 5 to 15 to 50, you'll be thanking me for the advise!

I've coded an answer to the original question, written in PHP, and you can try the example here. I've included a call to the highlight_file function so that you can see the code ... and I've even commented my PHP!