PHP or HTML first or does it matter?

心已入冬 提交于 2019-12-23 10:16:54

问题


I have a possible stupid question, but I'll ask it anyway.

Does it matter what goes first, PHP or HTML code?

For example: Does PHP go before HTML, after HTML or does it matter at all?

<?php

echo "This is text";

?>


<html>
<head>
</head>
<body>
<center>
<font size="2">This is text</font>
</center>
</body>
</html>

Or:

<html>
<head>
</head>
<body>
<center>
<font size="2">This is text</font>
</center>
</body>
</html>

<?php

echo "This is text";

?>

Or:

<html>
<head>
</head>
<body>

<?php

echo "This is text";

?>

</body>
</html>

回答1:


The third one is the correct way (assuming you want the text to echo out in the body).

PHP can jump in and out of HTML as you have shown above.

<html>
<head>
</head>
<body>
<center>
<font size="2"><?php echo "This is text"; ?></font>
</center>
</body>
</html>



回答2:


Personally I put the PHP as much as possible at the top of the page or even better outside the html page completely by using the html pages as purely views in the MVC pattern.




回答3:


Add your php code before the html code.

This allows you to change the out type, set requied variables, add http response headers if you require, etc.

You can have a lot of php embeded tags in between the html.

The html in your question would be invalid, if you echoed output before or after . Make sure your out is valid html.

Don't be bad to the browser just cause they will try to work with whatever you give them.




回答4:


HTML doesn't go anywhere, but PHP script goes to server executes and response is returned to client side. Now that response is displayed/handled along with HTML code. HTML is only for browser where PHP script is used invoke service or do operations on database. So, first PHP(Server) and then HTML(Client).




回答5:


Onlything you have to maintain valid html structure. so you canot put anything outside the html tag. so third option is the most valid thing. but if you use any of others, it will print anything you want.




回答6:


Not being a php person, will try to ans this in general sense. HTML is for browsers and php is serverside. When your pages reaches to the browser, there is only HTML, while, if I am not wrong as php should behave similarly yo jsp, at serverside html is seen as simple strings that need to be printed out at stream. So ideally, this should not matter what come first.

From good practice perspective, as this is php code(in my case jsp) whose output will be html, I try to give more feel of java to my code file.



来源:https://stackoverflow.com/questions/10827545/php-or-html-first-or-does-it-matter

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!