Building a string made easier? I have these lines of code in my view:

&lt;?php
    $count = count($product-&gt;getTags());
    $tagsStr = '';
    foreach($product-&gt;getTags() as $key =&gt; $tag){
        $tagsStr.= " " . $tag-&gt;getTag();
        if(($key == 0 &amp;&amp; $count &lt; 1) || ($key == 0 &amp;&amp; $count &gt;1 &amp;&amp; $key != $count)){
            $tagsStr .= ',';
        }
    } ?&gt;


It prints strings like:


  Fish, Onions


Or 


  Fish, Onions, Eggs


All these items are stored in an ArrayCollection $product-&gt;getTags();.

I find that these are a lot of lines for completing something this simple. I was wondering if you have ideas on simplifying this code.
Peter Kiss's code works, but it's not fast.

&lt;?php

$tags = $product-&gt;getTags();
$c    = count($tags);
$str  = null;
for ($i = 0; $i &lt; $c; ++$i) {
    if ($str) {
        $str .= ", ";
    }
    $str .= $tags[$i]-&gt;getTag();
}

?&gt;


Yes, it’s not short (and not much different to your solution) but it’s highly efficient and therefor the solution I’d go for.