Four ways to mix PHP/HTML

高洛峰
Release: 2023-03-06 08:24:02
Original
1599 people have browsed it

PHP is a back-end language. In order to output it to the browser for the browser to display, it is inevitable to output HTML code. The following is an introduction to the three PHP/HTML hybrid methods I have used

1. Single/double quotation mark enclosing method

This is the most basic method. The usage is as follows

<?php  echo &#39;
 <!DOCTYPE html>
 
   
     <title> </title>
   
   
     <span>测试页面</span>
   
 
 ';
?>
Copy after login

This is the simplest method, directly 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=&#39;Hello!&#39;;
 echo "$Content";
 echo &#39;<br>';
 echo '$Content';
 ?>
Copy after login

Output

1 Hello!
2 $Content

It can be seen that the variable name in the string surrounded by double quotes is automatically parsed into the variable value, while surrounded by single quotes The variable name is still displayed

There are two disadvantages to writing this way

1. If the output content contains single/double quotes, it will be extremely difficult to process, because PHP cannot determine whether the quotes belong to the program or not. Output 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 is normal coloring, and the bottom is coloring surrounded by quotes

Four ways to mix PHP/HTML

2. Use HEREDOC/NOWDOC

HEREDOC and NOWDOC are new features that PHP 5.3 has started to support. It allows a custom identifier to be used to surround text in the program. The relationship between HEREDOC and NOWDOC is similar to that between double quotes and single quotes. , the former parses the variables in the block, while the latter does not parse the variables in the block

The usage of HEREDOC and NOWDOC is introduced below

<?php  $Content=&#39;Hello!&#39;;
 
 //下面写出了一个HEREDOC,其中标识LABEL可以自定义为任何字符串,但要保证开头的标识和结尾的标识一样
 echo <<<LABEL
 $Content
 LABEL;
 //结尾的方法:另起一行,打上LABEL。注意结尾的标识前面和后面不要插入任何字符,空格也不行
 
 echo &#39;<br>';//为了演示方便换行
 
 //NOWDOC和HEREDOC的书写方式差别在于NOWDOC的标识符需要用单引号包围
 echo 
Copy after login

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

Writing with HEREDOC/NOWDOC solves the problem of surrounding quotation marks, but it still does not solve the syntax coloring failure. Question

3. Embedding PHP program blocks in HTML (recommended)

This is a very suitable method, and this method is widely used in situations such as WordPress templates. middle. It is also more convenient to write. Just write the relevant code directly where you need to output it, like the following

<?php  
 //首先在这里写好相关的调用代码
 function OutputTitle(){
   echo &#39;TestPage&#39;;
 }
 function OutputContent(){
   echo &#39;Hello!&#39;;
 }
 
 //然后再下面调用相关函数就可以了
 ?>
 
 nbsp;html>
 
   
     <title><?php  OutputTitle(); ?></title>
   
   
     <span><?php  OutputContent(); ?></span>
   
 
Copy after login

I think this method is the best among the three methods, but doing this The disadvantage is that if there are too many such code blocks, it will seriously affect program reading.

4. Use front-end template engine

As the importance of the 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 the front-end / Back-end engineers can cooperate with each other to make the things developed by front-end development and back-end development more perfect, and gradually spawned a series of front-end template engines, 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 and learn about

The above summary of the four methods of PHP/HTML mixed writing is all the content shared by the editor. I hope it can give you a reference, and I hope you will support me a lot. PHP Chinese website.

For more articles related to the four methods of PHP/HTML mixed writing, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!