Home >Backend Development >PHP Problem >What are the ways to embed php into html?
1. Single/double quotation mark enclosing method
This is the most basic method. The usage is as follows:
<?php echo ' <!DOCTYPE html> <html> <head> <title> </title> </head> <body> <span>测试页面</span> </body> </html> '; ?>
This is the simplest method, just wrap it in single quotes.
As for the difference between double quotes and single quotes, it is that the former parses the variables within the quotes, while the latter does not parse the variables within the quotes. 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 surrounded by double quotes is automatically resolved to Variable value, while surrounding it with single quotes still displays the variable name.
There are two disadvantages to writing this way:
1. If the output content contains single/double quotation marks, it will be extremely difficult to process, because PHP cannot determine whether the quotation mark belongs to The program still outputs content, so an error will be reported.
2. Some modern text editors (such as SublimeText) will not be able to syntax color the output content surrounded by quotation marks. If there are some formatting problems, it will be extremely difficult to find. The picture is a screenshot of SublimeText3. The top one is the normal coloring, and the bottom one is the coloring surrounded by quotes.
2. Embed PHP program blocks in HTML (recommended)
This is a very suitable method. And this method is widely used in situations such as WordPress templates. It is also more convenient to write. Just write the relevant code directly where it needs to be output, as follows:
<?php //首先在这里写好相关的调用代码 function OutputTitle(){ echo 'TestPage'; } function OutputContent(){ echo 'Hello!'; } //然后再下面调用相关函数就可以了 ?> <!DOCTYPE html> <html> <head> <title><?php OutputTitle(); ?></title> </head> <body> <span><?php OutputContent(); ?></span> </body> </html>
I think this method is the best among the three methods, but this The disadvantage is that if there are too many such code blocks, it will seriously affect program reading.
3. Use front-end template engine
As the importance of front-end is increasing day by day in the entire web development, front-end/back-end engineers are gradually separated into two professions , so in order to ensure that front-end/back-end engineers can cooperate with each other and make the things developed by front-end development and back-end development more perfect, a series of front-end template engines have gradually been spawned, such as Smarty. The implementation code written using Smarty is very readable, which makes the separation of front/back end more efficient and convenient. Interested students can search to learn more.
Recommended tutorial: PHP video tutorial
The above is the detailed content of What are the ways to embed php into html?. For more information, please follow other related articles on the PHP Chinese website!