There are a few approaches:
- Push the strings into an array and use implode()
- Add the bits to a string, remove the last character, then echo the string
- 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.