0

Possible Duplicate:
String 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?

5
  • i think appending string is better, but then again its just my personal preference. Commented Jul 10, 2011 at 20:38
  • 1
    In it's current form, this question is quite subjective. Commented Jul 10, 2011 at 20:39
  • i wouldn't use the implode method as you may as well use the var concat, but there is noting wrong with using an array when its named like $output['page'] and concat that. Commented Jul 10, 2011 at 20:44
  • @esqew - I disagree. As with everything code related, proper benchmarking will reveal which is faster. Commented Jul 10, 2011 at 21:35
  • @kev - I see where you're coming from there, but the question "Is it... a better practice?" is really dependent on who you're asking. He didn't really specify a standpoint (i.e. memory usage/efficiency, etc.). Commented Jul 10, 2011 at 23:38

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.