Exploding and imploding…

Very often when I review other peoples PHP code I come across an old problem: generating a list of comma separated values from a list of things. This happens often when you want to, say, write an SQL query with a WHERE field IN (a,b,c,…,x) and you have an array with a, b, etc.

Another example is when you want to create an include path by concatenating a list of directories with the PATH_SEPARATOR. Yet another example is when you want to build a path concatenating them with the DIRECTORY_SEPARATOR.

The usual code to accomplish looks something like this:

<?php
     // loop through list an generate string
    $line = ;
    foreach( $list as $el ) {
        $line .= $el . ‘,’;
    }

    $line = substr( $line, 0, strlen( $line )1 );
?>
 

There is really nothing wrong with this code snippet. The last line is kind of ‘ugly’: while you are looping over the array and are concatenating a string with ‘,’ you don’t know which is the last element in the list. This means that when the loop is done you have an extra ‘,’ character. The solution: use substring to remove the final ‘,’. Another alternative is:

<?php
    …
    $line = preg_replace( ‘/,$/’, , $line );
?>
 

This way we use a regular expression to replace the last ‘,’ with a blank. A bit nicer than the substr() example. Both ways are ok but PHP has a really nice function called implode(). What implode() does is it takes a list or array and joins it with whatever character/s you want. So, the code we just wrote can be replaced with the much cleaner:

<?php
    …
    $line = implode( ‘,’, $list );
?>
 

With implode() you don’t need to do weird stuff to remove the last ‘,’ or whatever char/chars you used to concatenate the string. Another advantage is that the code is much faster since implode() is implemented in C code.

The implode() function has another useful and related function: explode(). This function takes a delim delimiter and a subject string and returns an array of strings which is obtained by splitting the subject using the delimiter. I’m quite sure you’ve used explode(). It’s used when you want to, for example take a comma separated list and parse it to obtain the fields. Of course if you want to parse a CSV file you should use another PHP function: fgetcsv(); but that is for another post…

So, the moral of this post should be: before embarking on doing weird things first take a look at the PHP library of functions. Chances are that someone has come across the same problem and have solved it and included it as a PHP function or library. The advantage is that these functions not only execute faster since they are straight C code but also have been tested by thousands of programmers and, in most cases, are known to work. This is often the case when you have to do operations on dates. For example: calculate what day of the month is next Friday, etc. This functions are quite tricky to write because there are a lot of border cases: leap years, change of month / year, etc. If you write
them your are quite likely to forget about some border case. You should always use library functions and particularly PHP library functions which have been extensively tested and used for years.