$age = 18;
if($age >=18){
echo '恭喜,你已成年,游戏时长不受限制';
}
//输出结果:恭喜。你已成年,游戏时长不受限制
$age = 15;
if($age >= 18){
echo '恭喜,你已成年,游戏时长不受限制';
}else{
echo '你未成年,游戏时长为2小时';
}
//输出结果:你未成年,游戏时长为2小时
$age = 20;
if($age >= 18 && $age <30){
echo $age.'岁,游戏时长没限制';
}else if( $age >= 30 && $age < 45){
echo $age.'岁,少玩点游戏';
}else if($age>=45){
echo $age.'岁,别玩游戏了,出去走走吧';
}else{
echo $age.'岁,别玩游戏,多陪陪家人';
}
//输出结果:20岁,游戏不受限制
$age = 15;
switch(true){
case $age >= 18 && $age <30:
echo $age.'岁,游戏时长没限制';
break;
case $age >= 30 && $age < 45:
echo $age.'岁,少玩点游戏';
break;
case $age>=45:
echo $age.'岁,别玩游戏了,出去走走吧';
break;
default:
echo $age.'岁,别玩游戏,多陪陪家人';
}
//输出结果:15岁,别玩游戏,陪陪家人
$colors = ['red','green','blue'];
$list = '<ul style="border:1px solid;background:lightcyan">';
$i = 0;
while($i<count($colors)){
$list .= '<li>{$colors[$i]}</li>';
$i =$i + 1;
}
$list .= </ul>;
echo $list;
$colors = ['red','green','blue'];
$list = '<ul style="border:1px solid;background:lightcyan">';
$i = 0;
do{
$list .= '<li>{$colors[$i]}</li>';
$i =$i + 1;
}while($i<count($colors));
$list .= </ul>;
echo $list;
$colors = ['red','green','blue'];
$list = '<ul style="border:1px solid;background:violet">';
for($i = 0 ; $i< count($colors);$i++){
$list .= "<li>{$colors[$i]}</li>";
}
$list .= </ul>;
echo $list;
<?php
$stus = [
['id' => 1, 'name' => '刘备', 'course' => 'js', 'score' => 83],
['id' => 2, 'name' => '关羽', 'course' => 'php', 'score' => 75],
['id' => 3, 'name' => '张飞', 'course' => 'js', 'score' => 52],
['id' => 4, 'name' => '孙权', 'course' => 'php', 'score' => 88],
['id' => 5, 'name' => '周瑜', 'course' => 'js', 'score' => 65],
['id' => 6, 'name' => '孔明', 'course' => 'php', 'score' => 53],
['id' => 7, 'name' => '赵云', 'course' => 'js', 'score' => 63],
['id' => 8, 'name' => '马超', 'course' => 'js', 'score' => 77],
['id' => 9, 'name' => '姜维', 'course' => 'php', 'score' => 93],
['id' => 10, 'name' => '黄忠', 'course' => 'js', 'score' => 81],
]
?>
<!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>PHP与HTML混编</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>
<caption>学生成绩表</caption>
<thead>
<tr>
<th>ID</th>
<th>姓名</th>
<th>课程</th>
<th>成绩</th>
</tr>
</thead>
<tbody>
<?php
foreach($stus as $stu){
// echo "<tr>";
// echo "<td>{$stu['id']}</td>";
// echo "<td>{$stu['name']}</td>";
// echo "<td>{$stu['course']}</td>";
// echo "<td>{$stu['score']}</td>";
// heredoc, 写模板, 可以解析内部变量
echo <<< STU
<tr>
<td>{$stu['id']}</td>
<td>{$stu['name']}</td>
<td>{$stu['course']}</td>
<td>{$stu['score']}</td>
</tr>
STU;
//!只查某种元素
// if($stu['course']==='php'){
// echo <<< STU
// <tr>
// <td>{$stu['id']}</td>
// <td>{$stu['name']}</td>
// <td>{$stu['course']}</td>
// <td>{$stu['score']}</td>
// </tr>
// STU;
// }
}
?>
</tbody>
</table>
</body>
</html>
相关推荐
© 2020 asciim码
人生就是一场修行