Cleaning up your PHP code with printf

Nov 18, 2012

I build a lot of WordPress sites, and I often work on code from other developers. I have seen a fair bit of PHP code and it always bothers me when I see code like this:



The reason I cringe when I see that is

  1. It is tough to quickly understand what is happening
  2. I know mantainence time for that style of coding is higher.

How I usually deal with code like that is to move the code into a printf() or sprintf() function. I am a fan of moving all variable assignments above the HTML code and then plugging the variables into the printf() function, like so.