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<?PHP2 Echo‘3 <! DOCTYPE html>4 5 6 <title> </title>7 8 <body>9 <span> test Page </span>Ten </body> One A‘; -?>
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 <? PHP 2 $Content= ' hello! ' ; 3 Echo "$Content"; 4 Echo ' <br> '; 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<?PHP2 $Content= ' hello! ';3 4 //The following is a heredoc that identifies a label that can be customized as any string, but is guaranteed to start with the same identity as the end.5 Echo<<<LABEL6 $Content7 LABEL;8 //the end of the method: another line, label. Note that the end of the logo does not insert any characters before and after, and no spaces9 Ten Echo' <br> ';//to illustrate the convenience of line wrapping One A //nowdoc and Heredoc differ in the way that Nowdoc identifiers need to be surrounded by single quotes - Echo<<< ' LABEL ' - $Content the LABEL; - //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<?PHP2 3 //first, write the relevant calling code here.4 functionOutputtitle () {5 Echo' Testpage ';6 }7 functionoutputcontent () {8 Echo' Hello! ';9 }Ten One //and then call the correlation function below. A?> - -<! DOCTYPE html> the - -<title><?php outputtitle ();?></title> - +<body> -<span><?php outputcontent ();?></span> +</body> AI 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 engineSince 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
Finishing Four ways to php/html mixed writing