html中使用php 的变量

html-css03

html中使用php 的变量,第1张

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混编的 ,如下图所示

PHP的变量是不可以传值给html的,只能是讲该变量的值通过html输出让浏览器显示给用户。

例如:

PHP中的如下代码

<?php

$a="hello,world"

echo "$a"

?>

大致相当于html的:

<html>

<body>

<p>hello,world<p>

</body>

</html>

补充:

因为PHP属于网站编程语言里的动态语言而html只是一个标记语言,不具备变量的运算等逻辑的处理,只管显示页面给用户。