开发者

Performance difference between these methods

Is there any advantages/disadvantages to either of these statements over the other one:

<?php 
$test = 1;
$test2 = 2;
$test3 = $test + $test2;
echo "<p>Hello World</p>"; 
?>

OR

<?php 
$test = 1;
$test2 = 2;
$test3开发者_JAVA技巧 = $test + $test2;
?>
<p>Hello World</p> 

What i'm asking is, if i'm outputting a page using PHP should i keep closing the php tags and stick normal HTML in or echo out the HTML? Which is better?


if you want do be realy exact, there are three options:

the slowest:

echo "<p>Hello World</p>"; 

a bit faster (no need to check for inline-valiables because of single quotes):

echo '<p>Hello World</p>'; 

the fastest (no php-interpreting at all):

<p>Hello World</p>

but between all of this, the difference would be so minimalistic that you won't ever notice it - much more important: make your code redable and do it the same way everywhere, so nobody who's reading your code (and has to maintain it) gets confused. i personally would prefer the third method (so i can use code-completition in my IDE), but it's your choice - i know a lot of people who output everything using echo.

EDIT: to be complete, there are some more possibilitys i didn't mentioned like heredoc- and nowdoc-syntax, but this are basically the same as double/single-quotes... also, you could write print instead of echo and so on, but that wouldn't make a difference.


Method 2 is cleaner IMHO because you separate PHP code from HTML. Your IDE (if you use any) can parse your HTML tags and autocomplete them, and spot any typo's.


I'm not a PHP programmer but I would assume the 2nd method is faster, because PHP doesn't have to process the echo language construct, allocate buffer and all that stuff. It is also cleaner, and less of a hassle to modify the HTML.

Also, it would be wise to learn to use a template engine for your HTML in order to separate concerns. Smarty was popular a couple years ago, I don't know if it's still is.


Although the difference is negligible, you should stick normal outputing out of PHP tags. Echo command will have to be parsed by PHP interpreter and then sent as output.


The only difference is that with echo(); you instruct PHP to process the code, otherwise, there is no difference at all.

One way or another, the result is exactly the same and for performance, there is almost no differences at all. Like... How much time PHP needs to process that echo();? I think with miliseconds you could run in problems calculating numbers that small. Hehe.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜