Commas in a PHP For Loop

When generating a string that lists items (e.g. for a MySQL query) with a for-loop in PHP, you often need to add commas after every element except the last one.

It may be trivial advice, but instead of having an if statement that checks whether this is the last element in the list, use PHP’s implode.

The above code takes the contents of the $FOO map and converts it into an SQL query (assuming in this example that all the values are stored as text).

Leave a Reply

Your email address will not be published. Required fields are marked *