欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

变量类型 遍历php多维数组 购物车

程序员文章站 2024-01-10 09:11:34
...

变量类型

4种标量类型 整型 字符串 布尔型 浮点型
2种复合型 数组 对象
2种特殊类型 resource null

整型 $int = 32;
字符串 定义在单引号、双引号、定界符中 $password = "qnfjklg123";
布尔型 $boolean = true;
浮点型$float = 12.12;
数组
$user = ['id' => 1, 'name' => '李四', 'email' => '123456789@qq.com'];
resource

  1. $handle = fopen('log.log','w')
  2. var_dump($handle);

null
$avatar = null;

数组遍历

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title><? echo "数组遍历" ?></title>
  8. </head>
  9. <body>
  10. <?php
  11. /**
  12. * foreach($arr as $k=>$v)
  13. * {
  14. * each $v['name'];
  15. * }
  16. */
  17. $navs = [
  18. ['id' => 1, 'name' => '前端相关'],
  19. ['id' => 2, 'name' => '后端相关'],
  20. ['id' => 3, 'name' => '微信相关'],
  21. ['id' => 4, 'name' => '辅助学习'],
  22. ['id' => 5, 'name' => 'phppadmin管理系统'],
  23. ];
  24. ?>
  25. </body>
  26. <div>
  27. <ul>
  28. <?php foreach ($navs as $k => $v) { ?>
  29. <li><?= $v['name'] ?></li>
  30. <?php } ?>
  31. </ul>
  32. <ul>
  33. <?php foreach ($navs as $k => $v) : ?>
  34. <li><?= $v['name'] ?></li>
  35. <? endforeach ?>
  36. </ul>
  37. <ul>
  38. <?php
  39. foreach ($navs as $v) {
  40. echo "<li>{$v['name']}</li>";
  41. }
  42. ?>
  43. </ul>
  44. </div>
  45. </html>
  46. <!-- php模板语法 为了省略流程控制中或者循环遍历中的{}对齐问题 -->
  47. <!-- foreach...endforeach if...endif switch...endswitch while...endwhile -->

购物车

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  7. <title>购物车</title>
  8. </head>
  9. <body>
  10. <?php
  11. $arr = [
  12. ['name' => '苹果', 'num' => 1, 'price' => 5],
  13. ['name' => '柚子', 'num' => 1, 'price' => 10]
  14. ];
  15. ?>
  16. <div>
  17. <table border="1" cellspacing="0">
  18. <thead>
  19. <tr>
  20. <th>品名</th>
  21. <th>数量</th>
  22. <th>价格</th>
  23. </tr>
  24. </thead>
  25. <tbody>
  26. <?php foreach ($arr as $key =>
  27. $value) { ?>
  28. <tr>
  29. <td>
  30. <?= $value['name'] ?>
  31. </td>
  32. <td>
  33. <?= $value['num'] ?>
  34. </td>
  35. <td>
  36. <?= $value['price'] ?>
  37. </td>
  38. </tr>
  39. <?php } ?>
  40. </tbody>
  41. <tfoot>
  42. </tfoot>
  43. </table>
  44. </div>
  45. </body>
  46. </html>