Page 1 of 1

Something that nags me

Posted: Sun May 17, 2009 5:50 pm
by socket1
Is there really any answer to this question?
Say you need to echo stuff in a loop

Code: Select all

foreach ($Array as $Data1 => $Data2)
is it best to do this:

Code: Select all

foreach ($Array as $Data1 => $Data2) {
?>
  <tr>
       <td><?php echo $Data1; ?></td>
       <td><?php echo $Data2; ?></td>
  </tr>
<?php
}
or this

Code: Select all

foreach ($Array as $Data1 => $Data2) {
echo '
  <tr>
       <td>'.$Data1.'</td>
       <td>'.$Data2.'</td>
  </tr>
';
}
To me the first way would be easier to code with since you wouldn't need to escape things.
Which would be faster or does it really matter?

Re: Something that nags me

Posted: Sun May 17, 2009 6:21 pm
by califdon
I really don't think the difference in execution could be measured.

Re: Something that nags me

Posted: Sun May 17, 2009 6:24 pm
by requinix
Doesn't matter. Slight difference that nobody - except those obnoxious people who argue about pre-increment versus post-increment - will notice.

By the way, your second bit of code isn't quite right.

Code: Select all

foreach ($Array as $Data1 => $Data2) {
echo "
 <tr>
      <td>$Data1</td>
      <td>$Data2</td>
 </tr>
";
}
Fixed the HTML too. Tsk tsk.
(The first bit is missing the opening { too)

Re: Something that nags me

Posted: Sun May 17, 2009 7:02 pm
by socket1
Ooooh, thanks. Don't worry I don't normally code like that. But fixed nonetheless.