html中插入php的方法

html-css04

html中插入php的方法,第1张

1、第一种是在HTML中加PHP。

<head>

<metahttp-equiv="Content-Type"content="text/htmlcharset=utf-8"/>

<metahttp-equiv="Content-Language"content="zh-CN"/>

<title>HelloWorld</title>

</head>

<body>

<?php

echo"Helloworld!这是正文"

?>

</body>

</html>

2、第二种用echo输出HTML。

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

<?php

if(!$_POST){

echo‘<formaction=""method="post">

服务器地址:<inputtype="text"name="host"value="localhost"/><br/>

数据库账号:<inputtype="text"name="user"value=""/><br/>

数据库密码:<inputtype="password"name="pwd"value=""/><br/>

指定数据库:<inputtype="text"name="db"value="test"/><br/>

<inputtype="submit"value="确定"/>

</form>‘

}

?>

3、第三种就是用(<<<)标记符了,这是在PHP168的模板代码中首次见到的。

<?php

print<<<EOT

<divclass="slidecont">{$label[deepblue_mainslide]}</div>

<divclass="newcontainter">

<divclass="head">{$label[deepblue_mainh1]}</div>

<divclass="cont"id="Tab1">{$label[deepblue_maint1]}</div>

<divclass="cont"id="Tab2">{$label[deepblue_maint2]}</div>

</div>

<ahref="$rs[url]"title="$rs[descrip]"target="_blank">$rs[name]</a>

EOT

?>

htmlspecialchars() 函数把预定义的字符转换为 HTML 实体。

语法:

htmlspecialchars(string,flags,character-set,double_encode)

预定义的字符是:

&(和号)成为 &amp

" (双引号)成为 &quot

' (单引号)成为 '

<(小于)成为 &lt

>(大于)成为 &gt

htmlspecialchars_decode() 函数把一些预定义的 HTML 实体转换为字符。

语法:

htmlspecialchars_decode(string,flags)

会被解码的 HTML 实体是:

&amp解码成 &(和号)

&quot解码成 " (双引号)

' 解码成 ' (单引号)

&lt解码成 <(小于)

&gt解码成 >(大于)