Correcting teacher:PHPz
Correction status:qualified
Teacher's comments:
<?php
//单分支
$b = 7;
if ($b < 6) {
echo "b是一个小于6的整数";
} else {
echo "b不是一个小于6的整数";
}
echo "<hr>";
$age = 18;
//多分支判断
if ($age < 18) {
echo "小朋友请回家";
} elseif ($age <= 30 && $age >= 18) {
echo "欢迎收看";
} elseif ($age <= 60 && $age >= 31) {
echo "注意休息";
} else {
echo "年龄太大了 还是不要看了";
}
//循环语句
$nums = [1, 2, 3, 4, 5];
$ul = '<ul style="color: red;" >';
$i = 0;
while ($i < count($nums)) {
$ul .= "<li>{$nums[$i]}</li>";
$i++;
}
$ul .= "</ul>";
echo $ul;
echo "<hr>";
// do while 循环 先执行再判断 无论对错先执行一次再进入判断
$ul = '<ul style="color:#00ff80;">';
$i = 0;
do {
$ul .= "<li>{$nums[$i]}</li>";
$i++;
} while ($i > count($nums));
$ul .= '</ul>';
echo $ul;
echo "<hr>";
// for 循环 中断与跳过
$ul = '<ul style="color:#0000ff;">';
for ($i = 0; $i < count($nums); $i++) {
//跳过执行
if ($i === 1) continue;
$ul .= "<li>{$nums[$i]}</li>";
}
$ul .= '</ul>';
echo $ul;
echo "<hr>";
// 中断执行
$ul = '<ul style="color:#0000ff;">';
for ($i = 0; $i < count($nums); $i++) {
//跳过执行
if ($i === 1) break;
$ul .= "<li>{$nums[$i]}</li>";
}
$ul .= '</ul>';
echo $ul;
echo "<hr>";
<?php
$nums = [1, 2, 3, 4, 5, 6, 7, 8];
?>
<!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>
/* 写一个css样式 */
.active {
background-color: red;
border: 1px solid pink;
}
</style>
</head>
<body>
<ul>
<?php foreach ($nums as $i) : ?>
<!-- 输出索引大于3的数 -->
<?php if ($nums[$i] > 3) : ?>
<!-- 判断一下 给索引小于6的也就是5之前的数加一个样式 -->
<?php $active = $nums[$i] < 6 ? "active" : ""; ?>
<li class=<?= $active ?>><?= $nums[$i] ?></li>
<?php endif ?>
<?php endforeach ?>
</ul>
</body>
</html>