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
socket1
Forum Commoner
Posts: 82 Joined: Mon Dec 08, 2008 7:40 pm
Location: Shokan, New York
Post
by socket1 » Sun May 17, 2009 5:50 pm
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.
califdon
Jack of Zircons
Posts: 4484 Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA
Post
by califdon » Sun May 17, 2009 6:21 pm
I really don't think the difference in execution could be measured.
requinix
Spammer :|
Posts: 6617 Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA
Post
by requinix » Sun May 17, 2009 6:24 pm
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
Post
by socket1 » Sun May 17, 2009 7:02 pm
Ooooh, thanks. Don't worry I don't normally code like that. But fixed nonetheless.