Removing last comma in PHP?

There are a few approaches:

  1. Push the strings into an array and use implode()
  2. Add the bits to a string, remove the last character, then echo the string
  3. Iterate more explicitly, and on the last iteration, don’t output the ‘,’

Of those options, I’d probably use #1, because it makes the code just a bit more self-documenting.

Leave a Comment