Something that nags me

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
socket1
Forum Commoner
Posts: 82
Joined: Mon Dec 08, 2008 7:40 pm
Location: Shokan, New York

Something that nags me

Post 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?
Last edited by socket1 on Sun May 17, 2009 7:03 pm, edited 3 times in total.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Something that nags me

Post by califdon »

I really don't think the difference in execution could be measured.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Something that nags me

Post 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)
socket1
Forum Commoner
Posts: 82
Joined: Mon Dec 08, 2008 7:40 pm
Location: Shokan, New York

Re: Something that nags me

Post by socket1 »

Ooooh, thanks. Don't worry I don't normally code like that. But fixed nonetheless.
Post Reply