2011-07-10 49 views
0

可能重複:
String concatenation vs array implode in PHP使用數組,而不是追加到字符串

有可以在其中構建字符串的情況下槽的功能,如:

$output = ''; 

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

... 


echo $output; 

你認爲最好使用一個數組,並在最後使用數組嗎?

$output = array(); 

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

... 


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

它被認爲是一種更好的做法嗎?

+0

我認爲附加字符串比較好,但是再次只是我個人的偏好。 – Ibu

+1

在現在的形式中,這個問題是相當主觀的。 – esqew

+0

我不會使用implode方法,因爲您可能也會使用var concat,但是在使用數組命名爲$ output ['page']和concat時,使用數組時不會出現錯誤。 –

回答

0

您可能會使用數組進行性能提升。 它大多數情況下它會小到不會導致問題。

但很難說,因爲你沒有提供任何背景。