Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

what is the recommended way to embed html codes inside php codes?

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

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

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...