变量类型 遍历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
$handle = fopen('log.log','w')
var_dump($handle);
null$avatar = null;
数组遍历
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><? echo "数组遍历" ?></title>
</head>
<body>
<?php
/**
* foreach($arr as $k=>$v)
* {
* each $v['name'];
* }
*/
$navs = [
['id' => 1, 'name' => '前端相关'],
['id' => 2, 'name' => '后端相关'],
['id' => 3, 'name' => '微信相关'],
['id' => 4, 'name' => '辅助学习'],
['id' => 5, 'name' => 'phppadmin管理系统'],
];
?>
</body>
<div>
<ul>
<?php foreach ($navs as $k => $v) { ?>
<li><?= $v['name'] ?></li>
<?php } ?>
</ul>
<ul>
<?php foreach ($navs as $k => $v) : ?>
<li><?= $v['name'] ?></li>
<? endforeach ?>
</ul>
<ul>
<?php
foreach ($navs as $v) {
echo "<li>{$v['name']}</li>";
}
?>
</ul>
</div>
</html>
<!-- php模板语法 为了省略流程控制中或者循环遍历中的{}对齐问题 -->
<!-- foreach...endforeach if...endif switch...endswitch while...endwhile -->
购物车
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>购物车</title>
</head>
<body>
<?php
$arr = [
['name' => '苹果', 'num' => 1, 'price' => 5],
['name' => '柚子', 'num' => 1, 'price' => 10]
];
?>
<div>
<table border="1" cellspacing="0">
<thead>
<tr>
<th>品名</th>
<th>数量</th>
<th>价格</th>
</tr>
</thead>
<tbody>
<?php foreach ($arr as $key =>
$value) { ?>
<tr>
<td>
<?= $value['name'] ?>
</td>
<td>
<?= $value['num'] ?>
</td>
<td>
<?= $value['price'] ?>
</td>
</tr>
<?php } ?>
</tbody>
<tfoot>
</tfoot>
</table>
</div>
</body>
</html>
上一篇: nginx常用命令持续更新
下一篇: 七牛云存储支持什么样的网站程序?