Tell me more ×
Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

What general tips do you have for golfing in PHP? I'm looking for ideas that can be applied to code golf problems in general that are at least somewhat specific to PHP (e.g. "remove comments" is not an answer). Please post one tip per answer.

share|improve this question
Wait, am I doing it right?... Anyway, I'm really curious about this one. PHP is used by many people and golfers, but I almost have no idea how to golf a PHP code. – JiminP Jun 20 '11 at 14:11
Use short tags <??> It can save a few bytes. – Mob Aug 12 '11 at 20:49

5 Answers

up vote 10 down vote accepted

Understand how variables and whitespace interact with PHP's language constructs.

In my (admittedly short) time golfing, I have found that PHP's language constructs (e.g. echo, return, for, while, etc) behave in a less-than-intuitive way when interacting with variables and whitespace.

echo$v;, for example, is perfectly valid, as are return$v; and other similar constructs. These small reductions in whitespace can lead to a significant cumulative decrease in length.

Keep in mind, though, that variables before language constructs require a space after, as in the following example:

foreach($a AS$b){}

Because AS is a language construct, a space is not required before the variable $b, but if one were to omit the space before it, resulting in $aAS, this would be parsed as a variable name and lead to a syntax error.

share|improve this answer

Use strings wisely.

This answer is two-fold. The first part is that when declaring strings, you can utilize PHP's implicit conversion of unknown constants to strings to save space, e.g:

@$s=string;

The @ is necessary to override the warnings this will produce. Overall, you end up with a one-character reduction.

is that sometimes, it may be space effective to set a variable to the name of an often used function. Normally, you might have:

preg_match(..);preg_match(..);

But when golfing, this can be shortened easily to:

@$p=preg_match;$p(..);$p(..);

With only two instances of "preg_match", you're only saving a single character, but the more you use a function, the more space you will save.

share|improve this answer

You don't always need to write out conditional checks. For example, some frameworks use this at the top of their files to block access:

<?php defined('BASE_PATH')||die('not allowed');

Or in normal functions

$value && run_this();

instead of

if($value) { run_this(); }
share|improve this answer

Learn a large subset of the library functions.

PHP's library is pretty huge and provides a ton of convenient functions that can greatly shorten various tasks. You could just search every time you try to do something, but beyond wasting time you might not find anything that matches your particular search. The best way is just to get familiar with the library and memorize function names and what they do.

share|improve this answer
2  
That's a lot of memorization, especially given the rather inconsistent naming of a whole lot of functions ;-) – Joey Jun 24 '11 at 20:49
@Joey Agreed. Akin to memorizing the Java library, except that would be arguably less useful since it's more verbose. – Matthew Read Jun 24 '11 at 20:54
1  
I find that the most important functions for the challenges I've come across so far here are the string manipulation and array manipulation functions. Creative use of those can really cut down the code. – migimaru Aug 12 '11 at 17:03

Regarding file I/O:

Linking to another related question, the answers to which fit here.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.