phpstringforeach

PHP: create string with values separated by comma BUT without comma at the end of string


I am using the following PHP snippet to echo data to an HTML page. This works fine so far and returns results like the following example: value1, value2, value3, value4, value5,

How can I prevent it from also adding a comma at the end of the string while keeping the commas between the single values ?

My PHP:

<?php foreach ($files->tags->fileTag as $tag) { echo $tag . ", "; } ?>

Solution

  • If

    $files->tags->fileTag
    

    is an array, then you can use

    $str = implode(', ', $files->tags->fileTag);