PHP/HTML mixed write methods

Source: Internet
Author: User
The following is a summary of four methods of PHPHTML hybrid writing. I think this is quite good. now I will share it with you and give you a reference. Let's take a look at PHP as a back-end language. in order to output the HTML code to the browser, it is inevitable to output the HTML code, the following describes three mixed PHP/HTML encoding methods I have used.

1. single/double quotation marks

This is the most basic method. its usage is as follows:

                 Test page ';?>

This is the simplest method. you can simply wrap it in single quotes.

The difference between double quotation marks and single quotation marks lies in that the former resolves the variables in the quotation marks, while the latter does not parse the variables in the quotation marks. See the example below.

 '; echo '$Content'; ?>

Output

1 Hello!
2 $ Content

It can be seen that the variable name in the string enclosed by double quotation marks is automatically resolved to the variable value, while the variable name is still displayed when surrounded by single quotation marks.

There are two disadvantages of this writing:

1. if the output content contains single or double quotation marks, it will be extremely difficult to handle. because PHP cannot determine whether the quotation marks belong to the program or the output content, an error will be reported.

2. writing modern text editors (such as SublimeText) will not be able to syntax the output content enclosed by quotation marks. if there are some format problems, it will be very difficult to find out. In the figure, it is a part of SublimeText3. the above is a normal coloring, and the following is a coloring surrounded by quotation marks.

2. use HEREDOC/NOWDOC

HEREDOC and NOWDOC are a new feature supported by PHP5.3, which allows a user-defined identifier to enclose text in a program, the relationship between HEREDOC and NOWDOC is similar to that between double quotation marks and single quotation marks. The former parses the variables in the block, while the latter does not parse the variables in the block.

The following describes how to use HEREDOC and NOWDOC

 '; // For convenience of line feed // The difference between NOWDOC and HEREDOC is that NOWDOC identifiers need to enclose echo in single quotes <'label' $ Content LABEL; // What are other differences?>

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

Writing with HEREDOC/NOWDOC solves the problem of enclose quotation marks, but still does not solve the problem of invalid syntax coloring.

3. embed PHP blocks in HTML (recommended)

This is a very suitable method and is widely used in scenarios such as WordPress templates. It is easy to write, just write the relevant code where the output is needed, just like below

            <? Php OutputTitle ();?>           
     

I think this method is the best among the three methods, but the disadvantage is that if there are more code blocks, it will seriously affect program reading.

4. use the front-end template engine

As the importance of front-end is increasing throughout Web development, front-end and back-end engineers are gradually being separated into two jobs. therefore, to ensure that front-end and back-end engineers can cooperate with each other, the integration of frontend development and backend development is more perfect, and a series of front-end template engines, such as Smarty, are gradually generated. The code written with Smarty is highly readable, which makes the separation of the front and back ends more efficient and convenient. If you are interested, you can search and learn.

The above summary of the four PHP/HTML mixed writing methods is all the content shared by the editor. I hope to give you a reference and support for PHP.

For more articles on four methods of mixed PHP/HTML writing, refer to the PHP Chinese website!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.