I have two bits of code that do the same thing:
foreach ($pdo as $var) { ?>
<div class="someclass"> <?php
echo $var["column"]; ?>
</div> <?php
}
//or
foreach ($pdo as $var) {
echo "<div class='someclass'>";
echo $var["column"];
echo "</div>";
}
The first is quicker to type, but the second looks cleaner. Is there any kind of performance difference between remaining in PHP versus weaving in and out through PHP and HTML? If there is no performance difference, which is 'correct', as in most commonly used?
echo 'something';is actually quicker thanecho "something";- when the outer quote marks are doubles ("..") then the contents is evaluated. So,$var='blah-blah'; echo "I say: $var";gives a different result to$var='blah-blah'; echo 'I say: $var';echo "<div class='someclass'>";will work fine, butecho "<div class="someclass">";will not.echo '<div class="someClass">';orecho '<div class=\'someClass\'>';- i.e ensure the outer-most quotes are singles, then use doubles inside, or you could escape the internal singles with a slash (\). - the first one is faster since there's no need to parse the escape slash.