Finishing Four ways to php/html mixed writing

Source: Internet
Author: User
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 quote surround method

This is the primary method, the usage is like this

1 
 
   4  5      6         7      8      9         test page ten     ';?>

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

1 
 
   '; 5 echo ' $Content '; 6?>

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 cannot 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 content of the output surrounded by quotes, which can be extremely difficult to find if some formatting problems occur. 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

1 
 
   ';//In order to demonstrate a convenient way to wrap a line of//nowdoc and Heredoc, the difference is that the Nowdoc identifier needs to surround the echo <<< ' LABEL ' with single quotes $Content 15 LABEL;16//other than?>

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.

PHP block embedded in 3.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

1 
 
       17         <?php outputtitle ();?>         21
  
         

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

  • 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.