1. 实例演示分支与循环, 不要抄源码
<?php
$arr = [
['id'=>1,'name'=>'Tom','age'=>18,'sex'=>0],
['id'=>2,'name'=>'Tom2','age'=>33,'sex'=>0],
['id'=>3,'name'=>'Tom3','age'=>10,'sex'=>1],
['id'=>4,'name'=>'Tom4','age'=>31,'sex'=>0],
['id'=>5,'name'=>'Tom5','age'=>18,'sex'=>2],
];
//if
$age = 22;
if($age > 18){
echo "恭喜你,成年了";
}
echo '<br>';
//if else
if($age >18){
echo "恭喜你,成年了";
}else{
echo "你还没有成年";
}
echo '<br>';
//if else 语法糖三元运算符
echo $age>18? "恭喜你,成年了":"你还没有成年";
echo '<br>';
//if else if ....else
if($age >18 & $age <30){
echo "恭喜你,成年了";
}elseif($age>=30){
echo "你已经大于或等于30岁了";
}else{
echo "你年龄低于18";
}
echo '<br>';
//语法糖switch
switch(true){
case $age<18: echo "你还小";
break;
case $age >=18 & $age <30: echo "你年青力壮";
break;
default:echo "你什么都没有";
}
echo '<br>';
//for循环
for($i = 0; $i<count($arr);$i++){
echo "你叫{$arr[$i]['name']},年龄是{$arr[$i]['age']}<br>";
}
//foreach 循环
foreach($arr as $key=>$value){
echo "你是{$value['name']},{$value['age']}岁了<br>";
}
2. 实例演示流程控制之模板语法, 自己测试switch
<?php
$arr = [
['id'=>1,'name'=>'Tom','age'=>18,'sex'=>0],
['id'=>2,'name'=>'Tom2','age'=>33,'sex'=>0],
['id'=>3,'name'=>'Tom3','age'=>10,'sex'=>1],
['id'=>4,'name'=>'Tom4','age'=>31,'sex'=>0],
['id'=>5,'name'=>'Tom5','age'=>18,'sex'=>2],
];
?>
<!-- 流程模板 -->
<!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;
}
</style>
</head>
<body>
<table border:1px;>
<caption>这是一个表格</caption>
<thead>
<tr>
<th>id</th>
<th>姓名</th>
<th>年龄</th>
<th>性别</th>
</tr>
</thead>
<tbody>
<?php foreach($arr as $key=>$value): ?>
<tr>
<td><?=$value['id']?></td>
<td><?=$value['name']?></td>
<td><?=$value['age']?></td>
<td><?=($value['sex']? '男':'女') ?></td>
</tr>
<?php endforeach ?>
</tbody>
</table>
</body>
</html>