Correcting teacher:PHPz
Correction status:qualified
Teacher's comments:
$age = 15;
if ($age >= 18) {
echo '恭喜, 已成年,可以观看<br>';
}
if ($age >= 18) {
echo '恭喜, 已成年,可以观看<br>';
} else {
//默认分支
echo '未成年, 请在家长陪同下观看<br>';
}
//如果分支只有一行代码,可以不写大括号
if ($age >= 18)
echo '恭喜, 已成年,可以观看<br>';
else
// 默认分支
echo '未成年, 请在家长陪同下观看<br>';
$age = 44;
if ($age >= 18 && $age < 30)
echo "{$age}岁, 彩礼, 能按揭吗? <br>";
else if ($age >= 30 && $age < 45)
echo "{$age}岁, 应该成个家了 <br>";
else if ($age >= 45)
echo "{$age}岁, 房贷快还完了 <br>";
else
// 未成年, < 18, 默认分支
echo "{$age}岁, 放学了, 我送你回家 <br>";
$age = 15;
switch (true) {
case $age >= 18 && $age < 30:
echo "{$age}岁, 彩礼, 能按揭吗? <br>";
break;
case $age >= 30 && $age < 45:
echo "{$age}岁, 应该成个家了 <br>";
break;
case $age >= 45:
echo "{$age}岁, 房贷快还完了 <br>";
break;
default:
echo "{$age}岁, 放学了, 我送你回家 <br>";
}
初始化循环变量,这里用索引当循环变量,$i=0;指向第一个数组元素
$i = 0;
循环条件:数组长度 = 最大索引 + 1
echo count($colors);
if ($i < count($colors)) {
echo $colors[$i] . '<br>';
}
更新循环条件
$i = $i + 1;
if ($i < count($colors)) {
echo $colors[$i] . '<br>';
}
$i = $i + 1;
if ($i < count($colors)) {
echo $colors[$i] . '<br>';
}
echo '<hr>';
总结循环三要素
$i =0;
$i < count($colors);
$i = $i + 1
while(),先判断再循环
$list = '<ul style="border:1px solid;background: lightcyan">';
$i = 0;
while ($i < count($colors)) {
$list .= "<li>{$colors[$i]}</li>";
// 更新条件
$i = $i + 1;
}
$list .= '</ul>';
echo $list;
do-while 与上面的区别在于条件判断的时机不同,先循环再判断
$list = '<ul style="border:1px solid;background: lightgreen">';
$i = 0;
do {
$list .= "<li>{$colors[$i]}</li>";
// 更新条件
$i = $i + 1;
} while ($i > count($colors));
$list .= '</ul>';
echo $list;
for 可看成 while 的语法糖
$list = '<ul style="border:1px solid;background: violet">';
for ($i = 0; $i < count($colors); $i++) {
$list .= "<li>{$colors[$i]}</li>";
}
$list .= '</ul>';
echo $list;
// 中断(break)或跳过某次循环(continue)
$list = '<ul style="border:1px solid;background: pink">';
for ($i = 0; $i < count($colors); $i++) {
// 只输出前二个
// if ($i > 1) break;
// 跳过第2个,只输出第1个和第3个
if ($i === 1) continue;
$list .= "<li>{$colors[$i]}</li>";
}
$list .= '</ul>';
echo $list;
$colors = ['red', 'green', 'blue'];
$colors = [0 => 'red', 1 => 'green', 2 => 'blue'];
printf('<pre>%s</pre>', print_r($colors, true));
$user = ['id' => 5, 'name' => '猪老师', 'score' => 90];
printf('<pre>%s</pre>', print_r($user, true));
echo $user['name'];
索引数组实际上关联数组的一个子集,只不过用了数字型的字符串
$colors = ['0' => 'red', '1' => 'green', '2' => 'blue'];
printf('<pre>%s</pre>', print_r($colors, true));
//下面两个都能获取到值green
echo $colors[1], '<br>';
echo $colors['1'], '<br>';
while,for
数组成员还可以是一个数组,多维数组: 二维数组
数据表的查询结果,就是用”二维数组”来表示
$users = [
0 => ['id' => 5, 'name' => '猪老师', 'gender' => 0, 'age' => 18],
1 => ['id' => 6, 'name' => '牛老师', 'gender' => 1, 'age' => 30],
2 => ['id' => 5, 'name' => '狗老师', 'gender' => 0, 'age' => 35],
];
$table = '<table border="1" width="400" cellspacing="0" cellpadding="3" align="center">';
$table .= '<caption>用户信息表</caption>';
$table .= '<thead bgcolor="#ccc"><tr><th>id</th><th>用户名</th><th>性别</th><th>年龄</th></tr></thead>';
$table .= '<tbody align="center">';
// 遍历这个二维数组
foreach ($users as $user) {
// $user 还是一个数组
print_r($user);
$table .= '<tr>';
$table .= '<td>' . $user['id'] . '</td>';
$table .= '<td>' . $user['name'] . '</td>';
// 1->true, 0->false
$table .= '<td>' . ($user['gender'] ? '女' : '男') . '</td>';
$table .= '<td>' . $user['age'] . '</td>';
$table .= '</tr>';
}
$table .= '</tbody></table>';
echo $table;
// 只查php
if ($stu['course'] === 'php') {
echo <<< STU <tr>
<td>{$stu['id']}</td>
<td>{$stu['name']}</td>
<td class="active">{$stu['course']}</td>
<td>{$stu['score']}</td>
</tr>
STU;
}
PHP模板写法 使用
//原本写法
<td><?php echo $stu['id'] ?></td>
//简化写法,省略了 php echo
<td><?= $stu['id'] ?></td>
endforeach
来表示结尾
<?php foreach ($stus as $stu) : ?>
//需要循环操作的代码
<?php endforeach ?>
<?php
// 用二维数组来模拟数据表查询结果集
$stus = [
['id' => 1, 'name' => '刘备', 'course' => 'js', 'score' => 83],
['id' => 2, 'name' => '关羽', 'course' => 'php', 'score' => 75],
['id' => 3, 'name' => '张飞', 'course' => 'js', 'score' => 52],
['id' => 4, 'name' => '孙权', 'course' => 'php', 'score' => 88],
['id' => 5, 'name' => '周瑜', 'course' => 'js', 'score' => 65],
['id' => 6, 'name' => '孔明', 'course' => 'php', 'score' => 53],
['id' => 7, 'name' => '赵云', 'course' => 'js', 'score' => 63],
['id' => 8, 'name' => '马超', 'course' => 'js', 'score' => 77],
['id' => 9, 'name' => '姜维', 'course' => 'php', 'score' => 93],
['id' => 10, 'name' => '黄忠', 'course' => 'js', 'score' => 81],
]
?>
<!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>php与html原生混编</title>
<style>
table {
border-collapse: collapse;
width: 360px;
text-align: center;
}
table th,
table td {
border: 1px solid #000;
padding: 5px;
}
table caption {
font-size: 1.3em;
}
table thead {
background-color: lightcyan;
}
.active {
color: red;
}
</style>
</head>
<body>
<table>
<caption>学生成绩表</caption>
<thead>
<tr>
<th>ID</th>
<th>姓名</th>
<th>课程</th>
<th>成绩</th>
</tr>
</thead>
<tbody>
<!-- 这里显示的用户数据 -->
<?php
foreach ($stus as $stu) {
// echo "<tr>";
// echo "<td>{$stu['id']}</td>";
// echo "<td>{$stu['name']}</td>";
// echo "<td>{$stu['course']}</td>";
// echo "<td>{$stu['score']}</td>";
// echo "</tr>";
// heredoc, 写模板, 可以解析内部变量
// echo <<< STU
// <tr>
// <td>{$stu['id']}</td>
// <td>{$stu['name']}</td>
// <td>{$stu['course']}</td>
// <td>{$stu['score']}</td>
// </tr>
// STU;
// 只查php
if ($stu['course'] === 'php') {
echo <<< STU
<tr>
<td>{$stu['id']}</td>
<td>{$stu['name']}</td>
<td class="active">{$stu['course']}</td>
<td>{$stu['score']}</td>
</tr>
STU;
}
}
?>
</tbody>
</table>
</body>
</html>
<?php
// 用二维数组来模拟数据表查询结果集
$stus = [
['id' => 1, 'name' => '刘备', 'course' => 'js', 'score' => 83],
['id' => 2, 'name' => '关羽', 'course' => 'php', 'score' => 75],
['id' => 3, 'name' => '张飞', 'course' => 'js', 'score' => 52],
['id' => 4, 'name' => '孙权', 'course' => 'php', 'score' => 88],
['id' => 5, 'name' => '周瑜', 'course' => 'js', 'score' => 65],
['id' => 6, 'name' => '孔明', 'course' => 'php', 'score' => 53],
['id' => 7, 'name' => '赵云', 'course' => 'js', 'score' => 63],
['id' => 8, 'name' => '马超', 'course' => 'js', 'score' => 77],
['id' => 9, 'name' => '姜维', 'course' => 'php', 'score' => 93],
['id' => 10, 'name' => '黄忠', 'course' => 'js', 'score' => 81],
]
?>
<!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>php流程控制的模板语法/替代语法</title>
<style>
table {
border-collapse: collapse;
width: 360px;
text-align: center;
}
table th,
table td {
border: 1px solid #000;
padding: 5px;
}
table caption {
font-size: 1.3em;
}
table thead {
background-color: lightcyan;
}
.active {
color: red;
}
</style>
</head>
<body>
<table>
<caption>学生成绩表</caption>
<thead>
<tr>
<th>ID</th>
<th>姓名</th>
<th>课程</th>
<th>成绩</th>
</tr>
</thead>
<tbody>
<!-- php模板语法的目标: html与php代码分离 -->
<?php foreach ($stus as $stu) : ?>
<!-- "{" => 冒号加php结束标记 -->
<!-- 当前已离开了php环境,处于html中 -->
<!-- 使用短标签进行简化: 只打印一个变量 -->
<!-- <tr>
<td><?php echo $stu['id'] ?></td>
<td><?php echo $stu['name'] ?></td>
<td><?= $stu['course'] ?></td>
<td><?= $stu['score'] ?></td>
</tr> -->
<!-- 只输出成绩大于70分 -->
<!-- <?php if ($stu['score'] > 70) : ?>
<tr>
<td><?php echo $stu['id'] ?></td>
<td><?php echo $stu['name'] ?></td>
<td><?= $stu['course'] ?></td>
<td class="active"><?= $stu['score'] ?></td>
</tr>
<?php endif ?> -->
<!-- 输出全部,并将不及格成绩描红 -->
<tr>
<td><?php echo $stu['id'] ?></td>
<td><?php echo $stu['name'] ?></td>
<td><?= $stu['course'] ?></td>
<?php $active = $stu['score'] < 60 ? "active" : '' ?>
<td class=<?= $active ?>><?= $stu['score'] ?></td>
</tr>
<!-- 动态设置样式的方法 -->
<?php endforeach ?>
</tbody>
</table>
</body>
</html>