How to use variables in php in html tags

WBOY
Release: 2016-07-06 13:53:20
Original
1585 people have browsed it

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>
Copy after login
Copy after login

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>
Copy after login
Copy after login

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

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