开发者

Difference between using regular HTML and HTML inside echo statements or variables in PHP

Hello I have always been thinking which is the best way of writing html in an MVC architecture.Would it be regular html as <div id="id1"> <input type="text" name="tBox" /></div> or echo '<div id="id1"> <input type="text" name="tBox" /></div>';. This is a very small example, but sometimes we can have a single variable or single echo statement having multiple lines of code.

Would we开发者_开发问答 have any performance issues since php has to be parse by the webserver (apache,iis etc), while HTML gets away and is rendered directly by the browser. So would we gain any parsing time by using regular HTML?


Regular straight HTML is preferable as it can be served statically without processing by PHP. That said however, if you've got an MVC architecture, then you shouldn't be echo'ing HTML in the first place -- that output should be handled by your view layer.


It doesn't matter at all when the file is a php file (and contains php code somewhere). The server executes it regardless of what's inside and pure plain text is returned the way it is and text that is echoed out is added in between (at the echo location). In the end a coherent plain text is produced and sent to the client (with the correct mime-type) which then makes the browser interpret it.

I usually put in longer blocks of static html outside of <?php .. ?> tags to make it more clear, but as soon as there is some logic involved, I echo everything out, to not break the php flow all the way.

But in the end, it is totally up to you.


I agree with Alex. You should keep your data and display layers separate. A popular solution would be to use a template system like Smarty (http://www.smarty.net). We use it at my work. Some people like it and some don't; you can decide that for yourself.


A static html has a higher performance than embedded html on echo or variables 'cause the html isnt proccessed by the server-side. So a useful implementation is to retrieve only necesary data vars, database rows, iterations, etc from the server-side because everything inside is gonna be proccessed by the php service.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜