开发者

Using a array instead of appending to string [duplicate]

This question already has answers here: Closed 11 years ago.

Possible Duplicate:

Strin开发者_如何学编程g concatenation vs array implode in PHP

there are situations in which you build the string trough a function like:

$output = '';

if($something) $output .= '<div>foo</div>';
if($something_else) $output .= '<div>moo</div>';

...


echo $output;

Do you think it's better to use a array instead and implode it at the end?

$output = array();

if($something) $output[] = '<div>foo</div>';
if($something_else) $output[] = '<div>moo</div>';

...


echo implode("\n", $output);

Is it considered a better practice?


You will probably take a performance hit using arrays. It most cases it would be small enough not to cause a problem.

But it's hard to say because you provide no context whatsoever.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜