September 24, 200917 yr When doing something like echoing out table row's in php is it better to group things as much as possible or do it lots of lines. For example Method 1: <?php echo "<tr><td class='row'>$mysqlvalue</td></tr>"; ?> Method 2: <?php echo "<tr>"; echo "<td class='row'>"; echo $mysqlvalue; echo "</td>"; echo "</tr>"; ?> This is obviously quite a basic example but I was thinking in more complex situations it might make a bit of a difference ?
September 24, 200917 yr When doing something like echoing out table row's in php is it better to group things as much as possible or do it lots of lines. For example Method 1: <?php echo "<tr><td class='row'>$mysqlvalue</td></tr>"; ?> Method 2: <?php echo "<tr>"; echo "<td class='row'>; echo $mysqlvalue; echo "</td>"; echo "</tr>"; ?> This is obviously quite a basic example but I was thinking in more complex situations it might make a bit of a difference ? Well firstly idk if you have noticed but you have one error in the code you forgot a quote at the end of the sec echo in example 2 and the best practice is example 2 though the first one would save you from having to write a few more echos but yea as for best practice its the sec.
September 24, 200917 yr Author Well firstly idk if you have noticed but you have one error in the code you forgot a quote at the end of the sec echo in example 2 and the best practice is example 2 though the first one would save you from having to write a few more echos but yea as for best practice its the sec. Yh slight mistake lol.. So would there not be any difference in the speed that either was executed ?
September 24, 200917 yr Yh slight mistake lol.. So would there not be any difference in the speed that either was executed ? Nah their would be no diff in your code speed wise their perfectly both good at using but im just use to using the sec one so i think it makes for better practice but eather ones perfectly fine hope this helped
September 24, 200917 yr You can use commas as a compromise point, you get both HTML layout and less echo calls: <?php echo "<tr>, <td class='row'>, $mysqlvalue, </td>, </tr>"; ?>
September 28, 200917 yr Even better: don't mix excess HTML and PHP: // break out of PHP ?> <tr> <td class="row"><?php echo $mysqlvalue; ?></td> </tr> <?php // and we're back again...
September 28, 200917 yr Author ^ Yh I know that but in actual case that I was using it that bit of code is inside an if statement so it gets echoed out for every record that is in the database... I see your point though.
September 28, 200917 yr performance-wise one echo is better than many comma separate echo statement is faster than concatenated (i.e .= ) type statements alternatively, use output-buffering.
September 28, 200917 yr What about heredocs? http://uk3.php.net/manual/en/language.types.string.php#language.types.string.syntax.nowdoc
September 28, 200917 yr Even better: don't mix excess HTML and PHP: // break out of PHP ?> <tr> <td class="row"><?php echo $mysqlvalue; ?></td> </tr> <?php // and we're back again... I second this. Also...a million echo is silly...this isn't dollhouse.
September 29, 200917 yr Author Yh I know from the example I've put it looks like I'm echo'ing it all out for no reason.. But where I'm actually using this it needs to be like that. Anyway question answered, thanks
September 29, 200917 yr I second this. Also...a million echo is silly...this isn't dollhouse. Dugg for Whedon
September 29, 200917 yr performance-wise one echo is better than many comma separate echo statement is faster than concatenated (i.e .= ) type statements alternatively, use output-buffering. Worrying about micro-optimisations normally detract attention from the bigger picture. If your app is so big that the fraction of a second saved by using a comma over "." is important, this probably isn't the forum to be asking questions on.
Create an account or sign in to comment