best way of printing data
for example i want to generate html code, using some data from database. here is two ways of printing data
<? echo '<li><img src="'.$row['image'].'" /></li>';?>
or
<? echo "<li><img src='$row[image]' /></li>";?>
both of them are working. and if so, why people use the first method, if without spliting the row by .
it works fine too.开发者_StackOverflow
thanks
The best way would be to use only one method.
I'd even use <?php echo '<li><img src="' . $row['image'] . '" /></li>'; ?>
There's a speed difference, but that would be barely measurable and usually not at all noticable. Every string in double quotes is parsed first. That's why it's better to not use double quotes for strings at all. If you use extremely many strings with vars in it, it could become a measurable difference - but doing that would be quite bad design in the first place.
Btw.: The same is true for often switching the parser on and off with <?php
and ?>
.
The main reason for doing the above is good coding practice.
Others may need to understand your script too. And maybe yourself too some years later. Vars in strings can be easier overlooked than vars included like this. Even more so on IDEs with syntax highlighting.
I've even seen people put a newline before every var inserted in this way. But IMHO that's a little too much. ;)
More or less offtopic: No I didn't read all the fighting going on in the other answers, but I know the old "no it's not slower" vs. 'Yes it is' kindergarden by heart. ;)
For christs sake, you're coders, damnit. Just test it:
<?php
$startt = microtime(true);
for ($i = 0; $i <= 10000000; $i++) {
$test = 'This is test number ' . $i;
// $test = "This is test number " . $i;
// $test = "This is test number $i";
}
$endt = microtime(true);
echo 'Used time: ' . ($endt - $startt);
?>
For me the first one gave 5.1321198940277, the second one 5.2075009346008 and the third one 6.4821639060974 (more than 1.2 secs difference). Q.E.D. so far.
The interesting thing would be to try that on different systems. Maybe I'll make my own question for this.
echo sprintf('<li><img src="%s" /></li>', $row['image']);
Much easier to read and less prone to errors if it get's changed/extended later.
Another method:
<li><img src="<?php echo($row['image']); ?>" /></li>
<? echo '<li><img src="'.$row['image'].'" /></li>';?>
A reasonable strategy is to output all static content in plain PHP templating. That way you don't have to worry about PHP string literal escaping in the static content:
<li><img src="<? echo $row['image']; ?>"/></li>
However, (a) you need to use htmlspecialchars()
on all raw-text strings output into HTML, or you've got potential cross-site-scripting issues. Also, <?
short tags should generally be avoided as they may not be enabled on all servers.
To cut down on the amount of typing “htmlspecialchars” in templates, define a shortcut function:
<?php
function h($s) {
echo htmlspecialchars($s, ENT_QUOTES);
}
?>
<li><img src="<?php h($row['image']); ?>"/></li>
In the second method:
<? echo "<li><img src='$row[image]' /></li>";?>
you cannot use functions, you can only use variables. It does not matter which method you use if you are printing variables.
the fastest way is to call the echo
function with multiple parameters instead of a string that contains the concatenated parameters:
echo '<li><img src="',$row['image'],'" /></li>';
is the equivalent of
echo '<li><img src="';
echo $row['image'];
echo '" /></li>'
which is faster than
echo '<li><img src=";'.$row['image'].'" /></li>';
If you are using '
in your html tags for specifying attribute values then use "
in PHP and if you are using "
in html then use '
. Either way you won't need to escape the quote in html by \
精彩评论