|
Posted by gosha bine on 05/14/07 20:35
NoWhereMan wrote:
> Maybe a stupid question.
>
>
> -------------------------
> 1
> -------------------------
> $str = '';
>
> for($i=0; $i<10; $i++)
> $str .= $i;
>
> echo $str;
>
> -------------------------
> 2
> -------------------------
> $str = array();
>
> for($i=0; $i<10; $i++)
> $str[]= $i;
>
> echo implode('', $str);
>
> -------------------------
>
> I suppose the latter will be more performant because there's less work for
> the garbage collector, am I right?
>
> thanks
>
I think performance differences are hardly noticeable here, but the
latter method is still far better. In real life we use non-empty
separators and mostly omit the last one: "a" + "b" + "c" -> "a,b,c".
implode() does exactly this, with concatenation we will need some ugly
cleanup at the end of the loop.
--
gosha bine
extended php parser ~ http://code.google.com/p/pihipi
blok ~ http://www.tagarga.com/blok
Navigation:
[Reply to this message]
|