Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the methods of mixing PHP/HTML

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "what are the methods of PHP/HTML mixing". In the daily operation, I believe that many people have doubts about the methods of PHP/HTML mixing. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "what are the methods of PHP/HTML mixing?" Next, please follow the editor to study!

1. Single / double quotation mark enclosing method

This is the most basic method, and the usage is like this.

This is the easiest way. Just wrap it in single quotation marks.

As for the difference between double quotation marks and single quotation marks, the former parses variables within quotation marks, while the latter does not resolve variables within quotation marks, see the following example

Output

1 Hello!

2$ Content

Thus it can be seen that the variable name in the string enclosed in double quotes is automatically resolved to the variable value, while the variable name is still displayed in single quotation marks.

There are two disadvantages of writing in this way.

1. If the output contains single / double quotes, it will be extremely difficult to deal with, because PHP cannot determine whether the quotation marks belong to the program or the output, so it will report an error.

2. Writing some modern text editors (such as SublimeText) in this way will not be able to syntactically color the output surrounded by quotation marks, and it will be extremely difficult to find out if there are some formatting problems. The picture shows a screenshot of SublimeText3, with the normal coloring above and the coloring enclosed in quotation marks below

2. Use HEREDOC/NOWDOC

HEREDOC and NOWDOC are new features that PHP5.3 began to support, which allow the use of a custom token to surround text in a program, while the relationship between HEREDOC and NOWDOC is similar to double quotation mark enclosing and single quotation mark enclosing. The former parses variables within blocks, while the latter does not parse variables within blocks.

Here is the use of HEREDOC and NOWDOC

You can also refer to the wiki: https://wiki.php.net/rfc/heredoc-with-double-quotes on PHP.net about these two.

Writing in HEREDOC/NOWDOC solves the problem of enclosing quotation marks, but it still doesn't solve the problem of syntax coloring failure.

3. Embed PHP blocks in HTML (recommended)

This is a very appropriate approach, and it is widely used in situations such as WordPress templates. It is also convenient to write, just write the relevant code directly where the output is needed, like the following

I think this method is the best of the three, but the disadvantage is that too many blocks of code will seriously affect the reading of the program.

4. Use the front-end template engine

Due to the increasing importance of the front end in the whole Web development, front-end / back-end engineers are gradually separated into two professions, so in order to ensure that the front-end / back-end engineers can cooperate with each other and make the front-end development and back-end development more perfect, a series of front-end template engines, such as Smarty, have gradually emerged. The implementation code written in Smarty is very readable, which makes the front / back end separation more efficient and convenient. Students who are interested can search and understand.

At this point, the study of "what are the methods of PHP/HTML rewriting" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report