Home >Backend Development >PHP Tutorial >How to use variables in php in html tags

How to use variables in php in html tags

WBOY
WBOYOriginal
2016-07-06 13:53:201608browse

Use php variables in the html tag, prompt: Undefined index: uid in /var/www//list.php
, the specific code of list.php is as follows:

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

Reply content:

Use php variables in the html tag, prompt: Undefined index: uid in /var/www//list.php
, the specific code of list.php is as follows:

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

Can’t I just press isset before echo

The first child element of your array is empty and the uid cannot be found

In many cases in PHP, one thing that needs attention is to have a fault-tolerant mechanism

Statement:
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