html中插入php的方法

html-css018

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

?>

html标签使用php中的变量方法如下:

一、如果html标签中使用php变量,提示:Undefined index: uid in /var/www//list.php ,list.php的具体代码如下:

<?php

require 'redis.php'

for ($i=0 $i <= ($redis->get("userid"))  $i++){

$data[] = $redis->hgetall("user:".$i)

}

/* var_dump($data) 的结果如下:

array (size=3)

0 =>

array (size=0)

empty

1 =>

array (size=4)

'uid' => string '1' (length=1)

'username' => string 'jjj' (length=3)

'password' => string '123' (length=3)

'age' => string '20' (length=2)

2 =>

array (size=4)

'uid' => string '2' (length=1)

'username' => string 'lamp' (length=4)

'password' => string '123' (length=3)

'age' => string '20' (length=2)

*/

?>

<table border="1">

<caption>user list</caption>

<tr>

<th>uid</th>

<th>username</th>

<th>age</th>

</tr>

<?php foreach ($data as $v) {?>

<tr>

<td><?php echo $v['uid'] ?></td>

<td><?php echo $v['username'] ?></td>

<td><?php echo $v['age'] ?></td>

</tr>

<?php } ?>

</table>

这时在echo 前 isset 下就可以了,代码如下:

<?php $a = 10 if( isset($a) ) { echo"这个变量存在" } else{echo"这个变量不存在" }?>

二、 php 可以和html混编的 ,如下图所示