Playing golf with PHP

One of my geekiest hobbies is playing golf. Not the 18 holes of weathered drudgery kind, but the 18 hours of staring at the same 2 lines of code kind. If you still don't know what I'm talking about, the aim of the game is solve a given problem in the smallest amount of code (fewest strokes... geddit?).

Code golf has been played with various languages since at least the '50s, according to the famous Perlgolf History book, but it was certainly the Perl golfers who took it to the next level. One might say Perl is perfectly suited for golf because of its perversely rich and succinct syntax, but I think golf's popularity in the Perl world says more about the mentality of the people who inhabit it. I can't imagine many VB.NET coders getting into it, even if their language wasn't so verbose.

Two years ago, 29 degrees launched a brand new golfing website, codegolf.com. The slick interface was a major attraction, no fiddling with test files required, just submit your code and see if it works. However, codegolf.com's biggest feature is that Perl, PHP, Ruby and Python can all compete on the same challenges together. While your PHP entries are unlikely to be troubling the top Perl entries, there's a fairly good chance of embarrassing some Ruby and Python coders ;-)

Since the PHP world is pretty new to all this, I've assembled some tips and tricks you can use to shorten your PHP solutions according to the codegolf.com rules. Hopefully needless to say, these should never get near any kind of production code.

  • Get a working version first

    This might seem like stating the obvious, but it's easy to miss a subtle error in a test case and then waste time golfing you code you have to throw away.

  • Notices? What notices?

    Notices will be ignored, so you can skip setting variables to 0, null, "" or array() at the beginning of the script. This also means you can drop the quotes from some strings.

  • The function with the shortest name may not be the shortest

    Don't always fixate on the functions with the shortest names, quite often a function with a longer name will save work elsewhere.

  • Lookup tables

    You can often cut out a large part of code by looking the result up from a string rather than calculating it. Consider using pack() to squeeze more data into the same string. Aim to produce an alphanumeric string, so you can drop the quotes.

  • Looping

    Foreach loops are rarely the shortest. While and for loops are the same length, but for gives you two free semi-colons so it usually wins. Always try keep loops to one statement, so you can drop the braces.

  • There's always another algorithm

    If you've spent ages on one approach and are still 50 bytes behind the guy ahead, it's probably time to think of a new algorithm.

  • Bitwise tricks

    Bitwise OR with 0 is a nice alternative to floor(), so floor($a / $b) becomes $a / $b | 0. Changing the case of a letter in a string is another good one - $c & "\xDF" is the same is strtoupper($c), and $c | " " is the same as strtolower($c).

  • Use literal characters instead of escapes

    While golfing it's easiest to keep characters escaped, but once you're ready to submit you can often save an extra few bytes by replacing escapes such as \n with a literal newline.

Beware, code golf is highly addictive and can lead to broken keyboards, windows and families.

Leave a Comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.