1.流程控制
1.1 分支判断
<?php
namespace _0809;
$a = 10;
$b = 20;
// 1.单分支
echo "1.单分支: ";
if ($a < $b) {
echo " $b 大于 $a <hr>";
}
// 2.双分支
echo "2.双分支: ";
$b = 5;
if ($a < $b) {
echo " $b 大于 $a <hr>";
} else {
echo " $b 小于 $a <hr>";
}
// 3.双分支语法糖:三元运算符
echo "3.双分支语法糖: 三元运算符 : ";
echo $a < $b ? " $b 大于 $a <hr>" : "$b 小于 $a <hr>";
// 4.多分支
echo "4.多分支: ";
$b = 10;
if ($a < $b) {
echo " $b 大于 $a <hr>";
} elseif ($a > $b) {
echo " $b 小于 $a <hr>";
} else {
echo " $b 等于 $a <hr>";
}
// 5.多分支语法糖:switch
echo "5.多分支语法糖: switch : ";
switch (true) {
case $a < $b:
echo " $b 大于 $a <hr>";
break;
case $a > $b:
echo " $b 小于 $a <hr>";
break;
default:
echo " $b 等于 $a <hr>";
}
1.2 循环
<?php
namespace _0809;
$arr = ["张三", "男", "zhangsan@qq.com", "贵州安顺", 18];
// 1.for循环
echo '1.for: <br>';
for ($i = 0; $i < count($arr); $i++) {
//不输出索引为1的值
if ($i === 1) {
continue;
}
echo $arr[$i] . '<br>';
}
// 2.foreach
echo '<hr>2.foreach: <br>';
foreach ($arr as $k => $v) {
printf('key: %s => value: %s <br>', $k, $v);
}
// 3.while
echo '<hr>2.while: <br>';
$i = 0;
while ($i < count($arr)) {
echo $arr[$i] . '<br>';
$i++;
}
2.模板语法
foreach渲染
<!DOCTYPE html>
<html lang="en">
<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>
<style>
table {
border-collapse: collapse;
/* width: 360px; */
text-align: center;
margin: auto;
}
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>
<div class="app">
<table>
<caption>学生信息表</caption>
<thead>
<tr>
<th><input type="checkbox" name="" id=""></th>
<th>ID</th>
<th>姓名</th>
<th>性别</th>
<th>年龄</th>
<th>入学时间</th>
<th>课程</th>
<th>班级</th>
<th>地址</th>
<th>邮箱</th>
</tr>
</thead>
<tbody>
<?php
$stuInfo = [
['id' => 1, 'name' => "赵大", 'sex' => 0, 'age' => 18, 'studyDate' => '2022.9', 'major' => 'PHP', 'class' => '2201', 'address' => '贵州贵阳', 'email' => 'zhaoda@qq.com'],
['id' => 2, 'name' => "王二", 'sex' => 0, 'age' => 18, 'studyDate' => '2022.9', 'major' => 'PHP', 'class' => '2201', 'address' => '贵州遵义', 'email' => 'wanger@qq.com'],
['id' => 3, 'name' => "张三", 'sex' => 1, 'age' => 18, 'studyDate' => '2022.9', 'major' => 'PHP', 'class' => '2201', 'address' => '贵州六盘水', 'email' => 'zhangsan@qq.com'],
['id' => 4, 'name' => "李四", 'sex' => 0, 'age' => 19, 'studyDate' => '2021.9', 'major' => 'Java', 'class' => '2101', 'address' => '贵州安顺', 'email' => 'lisi@qq.com'],
['id' => 5, 'name' => "钱五", 'sex' => 1, 'age' => 19, 'studyDate' => '2021.9', 'major' => 'Python', 'class' => '2102', 'address' => '贵州毕节', 'email' => 'qianwu@qq.com'],
['id' => 6, 'name' => "吕六", 'sex' => 1, 'age' => 20, 'studyDate' => '2021.9', 'major' => 'Python', 'class' => '2102', 'address' => '贵州铜仁', 'email' => 'lvliu@qq.com'],
];
?>
<?php foreach ($stuInfo as $stu) : ?>
<tr>
<td><input type="checkbox" name="" id=""></td>
<td><?= $stu['id'] ?></td>
<td><?= $stu['name'] ?></td>
<!-- 0:女 1:男 -->
<td><?= $stu['sex'] ? '男' : '女' ?></td>
<td><?= $stu['age'] ?></td>
<td><?= $stu['studyDate'] ?></td>
<td><?= $stu['major'] ?></td>
<td><?= $stu['class'] ?></td>
<td><?= $stu['address'] ?></td>
<td><?= $stu['email'] ?></td>
</tr>
<?php endforeach ?>
</tbody>
</table>
</div>
</body>
</html>
switch渲染
<body>
<form action="" method="POST">
<label>请输入您的年龄:</label>
<input type="number" name="text" id="">
<input type="submit" value="确定" .name='sub'>
<br>
<br>
</form>
<?php
if (empty($_POST['sub'])) {
$age = $_POST['text'];
switch (true) {
case $age > 7 && $age <= 12:
echo " <span>你现在是 $age 岁,应该在读小学!</span>";
break;
case $age > 12 && $age <= 18:
echo " <span>你现在是 $age 岁,应该在读中学!</span>";
break;
case $age > 18 && $age <= 28:
echo " <span>你现在是 $age 岁,应该在读大学或者研究生!</span>";
break;
case $age > 28 && $age <= 38:
echo " <span>你现在是 $age 岁,应该结婚了,同时应该在还房贷了!</span>";
break;
case $age > 38 && $age <= 58:
echo " <span>你现在是 $age 岁,房贷还完了,应该要操心你儿子的彩礼钱了和房贷了!</span>";
break;
case $age > 58 && $age <= 98:
echo " <span>你现在是 $age 岁,儿子孝顺,天天打麻将,不孝顺天天骂儿子</span>";
break;
case $age > 100 || $age <= 0:
echo " <span>$age 岁 是一个非法年龄</span>";
break;
default:
echo " <span>你现在是 $age 岁,还是个宝宝";
}
}
?>
</body>