Summary of four ways to php/html mixed writing

Source: Internet
Author: User
The following small series for everyone to bring a php/html mixed write four ways to summarize. Small series feel very good, now share to everyone, also for everyone to make a reference. Let's take a look at it with a little knitting.

PHP as a back-end language, in order to output to the browser to show the browser, there is no way to output HTML code, the following describes the three kinds of php/html I have used the mixed method

1, single/double quotes surround method

This is the primary method, the usage is like this

<?php Echo ' <! DOCTYPE html> 

This is the simplest way, directly wrapped in single quotation marks on the line.

As for the difference between double quotation marks and single quotes, it is that the former parses the variables in the quotation marks, and the latter does not parse the variables in the quotation marks, see the example below

<?php $Content = ' hello! '; echo "$Content"; Echo ' <br> '; Echo ' $Content ';?>

Output

1 hello!
2 $Content

Thus, the variable names in a string surrounded by double quotes are automatically parsed for the value of the variable, and the variable name is still displayed in single quotes

There are two drawbacks to writing this way.

1, if the output contains single/double quotation marks will be extremely difficult to handle, because PHP can not determine whether the quotation mark is a program or output content, it will be an error

2. Writing some modern text editors (such as Sublimetext) will not be able to syntax the contents of the output surrounded by quotes, and it will be extremely difficult to find any formatting problems. The picture is a SublimeText3, the above is the normal coloring, the following is surrounded by the color of quotation marks

2. Using Heredoc/nowdoc

Heredoc and Nowdoc are a new feature that PHP5.3 is starting to support, which allows a custom marker to surround the text in the program, while the Heredoc and nowdoc relationships are similar to double quotes surrounded by single quotes, which parse the variables within the chunk, And the latter does not parse the variables within the chunk

The following describes the usage of Heredoc and Nowdoc

<?php $Content = ' hello! ';  The following writes out a Heredoc, where the identity label can be customized as any string, but to guarantee the beginning of the identity and the end of the identity as Echo <<<label $Content LABEL; The end of the method: another line, label. Note that the end of the logo before and after not inserting any characters, space is not the  echo ' <br> ';//To demonstrate ease of wrapping  //nowdoc and Heredoc are written in a way that differs in that the Nowdoc identifier needs to enclose the echo in single quotes <<< ' label ' $Content label; Other  ?>

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

The problem of enclosing quotation marks is solved with heredoc/nowdoc, but the problem of grammatical coloring is still not solved.

3. Embed PHP blocks in HTML (recommended)

This is a very suitable approach, and this method is widely used in such as WordPress templates and other occasions. Writing is also more convenient, directly in the need to write the output of the relevant code on the line, just like the following

<?php  //First write here the relevant calling code function Outputtitle () {   echo ' testpage ';} function Outputcontent () {   echo ' Hello! '; }  //And then call the correlation function below to?>  <! DOCTYPE html> 

I think this method is the best of the three methods, but the disadvantage is that if such a block of code is too much, it will seriously affect the program reading.

4. Using the front-end template engine

Since the importance of the front-end is increasing throughout web development, the front/back-end engineers are now separating into two occupations, so to ensure that front/back-end engineers can work with each other, so that front-end development and back-end development of things docking more perfect, gradually spawned a series of front-end template engine, such as Smarty. The implementation code written with Smarty is very readable, which makes the separation of the front/back end more efficient and convenient. Interested students can go to the search to understand

The above is the whole content of this article, I hope that everyone's study has helped.


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.