Archive for April, 2010

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 );
?>
 

(more…)