Four Methods of PHP/HTML hybrid writing and four methods of php

Source: Internet
Author: User

Four Methods of PHP/HTML hybrid writing and four methods of php

PHP is a backend language. to output HTML code to the browser for display, it is inevitable to output HTML code. The following describes three mixed PHP/HTML coding methods I have used.

1. single/double quotation marks

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

<? Php echo '<! DOCTYPE html> 

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

The difference between double quotation marks and single quotation marks lies inThe former parses the variables in quotation marks, while the latter does not parse the variables in quotation marks. For more information, see the example below.

<?php $Content='Hello!'; echo "$Content"; echo '<br>'; 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 resolves 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

<? Php $ Content = 'Hello! '; // A heredoc is written below, where the LABEL can be customized as any string, but ensure that the logo at the beginning is the same as the logo at the end echo <LABEL $ Content LABEL; // method to end with a LABEL. Note that do not insert any characters into the front and back of the logo at the end, nor do spaces support echo '<br> '; // to demonstrate the 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 // first write the relevant call code function OutputTitle () {echo 'testpage';} function OutputContent () {echo 'Hello! ';} // And then call the relevant function below?> <! DOCTYPE html> 

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 I hope you can provide more support for the customer's house.

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.