分支实例演示
<?php
// 流程控制:分支
// 1.单分支
$age = 10;
if($age >= 18){
echo '恭喜你,成年人';
}
// 2.双分支
$age = 10;
if($age >= 18){
echo '恭喜你,成年人';
}else{
echo '小学生,好好学习';
}
echo '<hr>';
// 分支只有一行代码,可以不写大括号
$age = 30;
if($age >= 18)
echo '恭喜你,成年人';
else
echo '小学生,好好学习';
echo '<hr>';
// 3.多分支
$age = 100;
if($age >= 18 && $age < 40 ){
echo "{$age}岁,改结婚了";
}else if($age >= 40 ) {
echo "{$age}岁,好好锻炼身体";
}else{
echo '小学生,好好学习';
}
echo '<hr>';
// 4.多分支的语法糖:switch
$age = 25;
switch(true)
{
case $age >= 18 && $age < 40 ;
echo "{$age}岁,改结婚了";
break;
case $age >= 40 ;
echo "{$age}岁,好好锻炼身体";
break;
default:
echo '小学生,好好学习';
}
循环实例演示
<?php
// 流程控制:循环
$car = ['BMW','volvo','haval'];
print_r($car);
echo '<br>';
echo $car[2] . '<br>';
// 1.初始化循环变量,用索引当循环变量
$i =0;
// 2.循环条件:
// 数组长度 = 最大索引+1
// echo count($car);
if($i < count($car))
{
echo $car[$i] . '<br>';
}
// 3.更新循环条件
$i =$i+1;
if($i < count($car))
{
echo $car[$i] . '<br>';
}
echo '<hr>';
// 简化循环改写:while()
$i = 0;
while($i < count($car))
{
echo $car[$i] . '<br>';
$i =$i+1;
}
// 案例列表
$list = '<ul style="border:1px solid;background:cyan;">';
$i = 0;
while($i < count($car))
{
$list .= "<li>{$car[$i]}</li>";
$i =$i+1;
}
$list .= '</ul>';
echo $list;
echo '<hr>';
$list = '<ul style="border:1px solid;background:red;">';
$i = 0;
do{
$list .= "<li>{$car[$i]}</li>";
$i =$i+1;
}while($i > count($car));
$list .= '</ul>';
echo $list;
// for改写
$list = '<ul style="border:1px solid;background:yellow;">';
for($i =0;$i < count($car);$i++)
{
$list .= "<li>{$car[$i]}</li>";
}
$list .= '</ul>';
echo $list;
// 中断:break
$list = '<ul style="border:1px solid;background:violet;">';
for($i =0;$i < count($car);$i++)
{
if($i >1) break;
$list .= "<li>{$car[$i]}</li>";
}
$list .= '</ul>';
echo $list;
// 跳过某次循环:continue;
$list = '<ul style="border:1px solid;background:violet;">';
for($i =0;$i < count($car);$i++)
{
if($i === 1) continue;
$list .= "<li>{$car[$i]}</li>";
}
$list .= '</ul>';
echo $list;
php模板语法与html混编实例演示
<?php
$users =[
['id'=>'1','name'=>'李白','course' =>'php','gender'=>0,'score'=>60],
['id'=>'2','name'=>'杜甫','course' =>'mysql','gender'=>1,'score'=>30],
['id'=>'3','name'=>'韩愈','course' =>'js','gender'=>0,'score'=>90],
['id'=>'4','name'=>'杜牧','course' =>'php','gender'=>1,'score'=>80],
];
?>
<!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>php模板语法</title>
</head>
<body>
<table border="1" width ="500" cellspacing ="0" cellpadding="3" aling ="center">
<caption>用户信息表</caption>
<thead>
<tr bgcolor = "#ccc">
<th>id</th>
<th>姓名</th>
<th>课程</th>
<th>性别</th>
<th>成绩</th>
</tr>
</thead>
<!-- 遍历 -->
<tbody align="center">
<?php foreach($users as $user) : ?>
<!-- “{” => 冒号加php结束标记 -->
<!-- <tr>
<td><?php echo $user['id'] ?></td>
<td><?php echo $user['name'] ?></td>
<td><?php echo $user['course'] ?></td>
<td><?php echo $user['gender']?'女' : '男' ?></td>
<td><?php echo $user['score'] ?></td>
</tr> -->
<!-- 仅输出成绩大于60分的 -->
<!-- <?php if ($user['score'] >= 60) : ?>
<tr>
<td><?php echo $user['id'] ?></td>
<td><?php echo $user['name'] ?></td>
<td><?php echo $user['course'] ?></td>
<td><?php echo $user['gender']?'女' : '男' ?></td>
<td><?php echo $user['score'] ?></td>
</tr>
<?php endif ?> -->
<!-- 输出全部,低于60分的标红 -->
<tr>
<td><?php echo $user['id'] ?></td>
<td><?php echo $user['name'] ?></td>
<td><?php echo $user['course'] ?></td>
<td><?php echo $user['gender']?'女' : '男' ?></td>
<?php $color = $user['score'] < 60 ?'style="color: red;"' : '' ?>
<td <?php echo $color ?>><?php echo $user['score'] ?></td>
</tr>
<?php endforeach ?>
</tbody>
</table>
</body>
</html>