分支与循环
// 单分支
$age = 19;
if($age >= 18){
echo '满18岁,已成年'. '<br>';
}
// 双分支
$age = 12;
if($age >= 18){
echo '满18岁,你已成年<br>';
} else{
echo '你未成年,请家长陪同<br>';
}
// 双分支语法糖: 三元运算符
echo $age >=18 ? "满18岁,你已成年" : "你未成年,请家长陪同";
echo '<br>';
// 多分支
$age = 49;
if ($age < 18){
echo '你还是努力上学吧<br>';
} elseif ($age = 18){
echo '买房的钱存够了吗<br>';
} elseif ($age > 50){
echo '房贷供完了吗';
}
// 多分支语法糖 switch
$age = 61;
switch(true){
case $age > 12 && $age <=24;
echo '还在上学奋斗中';
break;
case $age > 25 && $age <=40;
echo '还在苦逼打工';
break;
case $age > 41 && $age <=65;
echo '在公司养老中';
break;
case $age > 66 && $age <=80;
echo '退休中';
break;
}
模板语法
namespace _0809;
// 用二维数组来模拟数据表查询结果
$stus = [
['id' => 1, 'name' => '张三', 'gender' => '男', 'score' => 83],
['id' => 2, 'name' => '李四', 'gender' => '女', 'score' => 66],
['id' => 3, 'name' => '刘五', 'gender' => '男', 'score' => 42],
['id' => 4, 'name' => '钱六', 'gender' => '女', 'score' => 90],
['id' => 5, 'name' => '陈七', 'gender' => '男', 'score' => 55],
['id' => 6, 'name' => '赵八', 'gender' => '女', 'score' => 45],
['id' => 7, 'name' => '马九', 'gender' => '男', 'score' => 75],
['id' => 8, 'name' => '陆十', 'gender' => '男', 'score' => 88],
]
?>
<!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;
}
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>
<thead>
<tr>
<th>ID</th>
<th>姓名</th>
<th>性别</th>
<th>成绩</th>
</tr>
</thead>
<tbody>
<?php foreach($stus as $stu): ?>
<tr>
<td><?=$stu['id']?></td>
<td><?=$stu['name']?></td>
<td><?=$stu['gender']?></td>
<td><?=$stu['score']?></td>
</tr>
<?php endforeach ?>
</tbody>
</table>
</body>
</html>