Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Lets say I have 2 cases:

<?php
   print("html code goes here");
?>

v.s

<?php
?>

html codes goes here

<?php
?>

Would the performance for PHP interpreter in the first case be worse than the second one? (Due to the extra overhead of processing inside print function).

So, does anyone have a recommended way to insert html codes inside php codes?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
995 views
Welcome To Ask or Share your Answers For Others

1 Answer

Oh, for the sake of all those who edit your code later, please never put any significant amount HTML code inside a string and echo it out. In any language.

HTML belongs in HTML files, formatted and styled by your IDE or editor, so it can be read. Giant string blocks are the biggest cause of HTML errors I have ever seen.

Performance shouldn't matter too much, in this case, but I would assume the second would be faster, because it is streamed directly to the output or buffer.

If you want it to be easier to read, enable short tags, and write it like this:

?><b>blah blah blah</b><?

Plus, with short tags enabled, it's easier to echo out variables:

Hello, <?= $username ?>

If you are using this to generate some sort of reusable library, there are other options.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...