listen > understand > code > teach

Beware of extra Commas: PHP Function Argument Lists are not like Arrays

Take another look at the Drupal community's coding standard on Arrays.

A couple months ago I was working on an importer for a Drupal site where the originating data could be scripted in JavaScript. So I was writing PHP code (to upload the Drupal nodes) using JavaScript code (to access the original data). It occurred to me just how convenient it is to be able to write every array element the same way, with a comma after it and not have to write conditional code that makes the last entry comma-less.

Well, yesterday, I had the realization that adding a comma after the last argument in a function call causes PHP (5.2.11) to crash. This is particularly annoying if you can not debug the code because it is being executed by drupal_execute in a separate process. So, write PHP function calls like this:

bropimporter_create_image(
"january_february_2010 :: essays_35-1 :: img2",
$IMPORT_ROOT,
"images/january_february_2010/essays_35-1",
"img2.jpg",
"january/february 2010"
);

NEVER like this

$image_nids[] = bropimporter_create_image(
"january_february_2010 :: essays_35-1 :: img2",
$IMPORT_ROOT,
"images/january_february_2010/essays_35-1",
"img2.jpg",
"january/february 2010",
);

A comma after the last argument is a bad thing.

Drupal SEO