Home > Backend Development > PHP Problem > Three ways to embed html code in php programs

Three ways to embed html code in php programs

王林
Release: 2023-03-07 22:58:02
forward
4271 people have browsed it

Three ways to embed html code in php programs

The three methods for embedding html code into php programs are as follows:

The first is to add PHP

large sections of html to HTML In the code, in each place where php needs to be executed,

For example, line7-9:

 1 <head>
 2     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 3     <meta http-equiv="Content-Language" content="zh-CN" />
 4     <title>Hello World</title>
 5 </head>
 6 <body>
 7     <?php
 8     echo "Hello world!这是正文";
 9     ?>
10 </body>
11 </html>
Copy after login

The second method uses echo to output HTML.

Because some HTML elements have double quotes, the content output by echo is enclosed in single quotes to avoid errors and save the escaping step. For example, code like this:

(Learning video sharing: php video tutorial)

  <?php
      if(!$_POST){
      echo ‘<form action="" method="post">
      服务器地址:<input type="text" name="host" value="localhost" /><br />
      数据库账号:<input type="text" name="user" value="" /><br />
      数据库密码:<input type="password" name="pwd" value="" /><br />
      指定数据库:<input type="text" name="db" value="test" /><br />
      <input type="submit" value="确定"/>
      </form>‘;
     }
 ?>
Copy after login

Or this kind of code with escape symbols:

<?php
     echo "<input type=\"submit\" value=\"确定\"/>" ;
 ?>
Copy after login

The third way is to use the (<<<) tag, which was first seen in the template code of PHP168.

  <?php
      print <<<EOT
      <div class="slidecont">{$label[deepblue_mainslide]}</div>
     <div class="newcontainter">
          <div class="head">{$label[deepblue_mainh1]}</div>
          <div class="cont" id="Tab1">{$label[deepblue_maint1]}</div>
          <div class="cont" id="Tab2">{$label[deepblue_maint2]}</div>
      </div>
      <a href="$rs[url]" title="$rs[descrip]" target="_blank">$rs[name]</a>
 EOT; 
 ?>
Copy after login

The document between "<<The advantage is that it is convenient to output large sections of HTML, no escaping is required, and variables can be quoted.

But there is one more thing to note when using the (<<

The EOT tag here can be replaced with any tag, such as print <<

Related recommendations: php tutorial

The above is the detailed content of Three ways to embed html code in php programs. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
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