php分支
代码如下
<?php
echo "多分支<br/>";
$age = 128;
if($age >=18 && $age<35){
echo '可以吃糖啦!';
}
else if($age >=35 && $age<50){
echo '建议少吃糖!';
}
else if($age<18 || $age >60){
echo '不能吃糖!';
}
else{
echo '不建议吃糖!';
}
echo '<hr/>';
echo "单分支<br/>";
$scores = 60;
if($scores>=60){
echo "您的分数{$scores},及格啦!!!";
}
echo '<hr/>';
$scores = 55;
if($scores>=60){
echo "您的分数{$scores},及格啦!!!";
}else{
echo "您的分数{$scores},不及格,要多加强学习啦!!!";
}
echo '<hr/>';
echo "switch写法<br/>";
$age = 25;
switch (true) {
case $age >= 18 && $age < 30:
echo "{$age}岁可以吃糖啦<br>";
break;
case $age >= 30 && $age < 45:
echo "{$age}岁,建议少吃糖<br>";
break;
case $age<18 || $age >60:
echo "{$age}岁,不能吃糖<br>";
break;
default:
echo "{$age}岁,放学,我送你回家<br>";
}
?>
输出结果
php循环输出与模板写法
代码如下
<?php
namespace _0809作业;
$an_arrs =[
['id'=>1,'name'=>'安邦','age'=>18,'order'=>1, 'score'=>59 ],
['id'=>2,'name'=>'安福','age'=>18,'order'=>2, 'score'=>42 ],
['id'=>3,'name'=>'安歌','age'=>18,'order'=>3, 'score'=>61 ],
['id'=>4,'name'=>'安国','age'=>18,'order'=>4, 'score'=>73 ],
['id'=>5,'name'=>'安和','age'=>18,'order'=>5, 'score'=>89 ],
['id'=>6,'name'=>'安康','age'=>18,'order'=>6, 'score'=>46 ],
['id'=>7,'name'=>'安澜','age'=>18,'order'=>7, 'score'=>65 ],
['id'=>8,'name'=>'安民','age'=>18,'order'=>8, 'score'=>62 ],
['id'=>9,'name'=>'安宁','age'=>18,'order'=>9, 'score'=>52 ],
['id'=>10,'name'=>'安平','age'=>18,'order'=>10,'score'=>55],
['id'=>11,'name'=>'安然','age'=>18,'order'=>11,'score'=>88],
['id'=>12,'name'=>'安顺','age'=>18,'order'=>12,'score'=>60]
];
?>
<!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>学生信息管理系统</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>
<?php
?>
<table>
<caption>学生成绩表</caption>
<thead>
<tr>
<th>ID</th>
<th>姓名</th>
<th>年龄</th>
<th>学号</th>
<th>成绩</th>
</tr>
</thead>
<tbody>
<?php foreach ($an_arrs as $an) : ?>
<tr>
<td><?php echo $an['id']?></td>
<td><?php echo $an['name']?></td>
<td><?php echo $an['age']?></td>
<td><?php echo $an['order']?> </td>
<td><?php echo $an['score']?></td>
</tr>
<?php endforeach ?>
</tbody>
</table>
</body>
</html>
输出结果