php中怎么嵌入html代码?

PHPz
Release: 2020-06-18 10:07:22
Original
1927 people have browsed it

php中怎么嵌入html代码?

php中嵌入html代码的方法

方法1:用echo输出HTML

因为HTML有的元素中有双引号,所以用echo输出的内容用单引号括起来,避免出错,也省了转义这一步。比如这样的代码:

  <?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

或者这种加了转义符号的:

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

方法2:用(<<<)标记符了,这是在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

“<<优点是输出大段HTML方便,不需要转义,而且可以引用变量。

但是在使用(<<

这里的EOT标记可以替换成 任意标记 比如 print <<

相关教程推荐:php视频教程

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