Correcting teacher:天蓬老师
Correction status:qualified
Teacher's comments:少了作业总结, 下次注意
<?php
//一.分支举例
//车速
$speed = 200;
//罚款
$punishment = 200;
//1.单分支
if ($speed > 120){
$punishment = $punishment * 1;
}
echo '车速>' . $speed . 'km/h:' . '处罚' . $punishment . '<br>';
echo '<hr>';
//单分支模版语法
if ($speed > 120):
$punishment = $punishment * 1;
endif;
echo '车速>' . $speed . 'km/h:' . '处罚' . $punishment . '<br>';
echo '<hr>';
//2.双分支
if ($speed > 120){
$punishment = $punishment * 1;
}else {
$punishment = $punishment * 0;
}
echo '车速=' . $speed . 'km/h:' . '处罚' . $punishment . '<br>';
echo '<hr>';
//双分支模版语法
if ($speed > 120):
$punishment = $punishment * 1;
else:
$punishment =$punishment * 0;
endif;
echo '车速=' . $speed . 'km/h:' . '处罚' . $punishment . '<br>';
echo '<hr>';
//3.多分支
if ($speed > 120 && $speed <= 140){
$punishment = $punishment * 1;
}elseif ($speed > 140 && $speed <= 180) {
$punishment = $punishment * 1.2;
}elseif ($speed > 180) {
$punishment =$punishment *1.5;
}else {
$punishment =$punishment * 0;
}
echo '车速=' . $speed . 'km/h:' . '处罚' . $punishment . '<br>';
echo '<hr>';
//多分支模版语法
if ($speed > 120 && $speed <=140):
$punishment = $punishment * 1;
elseif ($speed >140 && $speed <= 180):
$punishment = $punishment *1.2;
elseif ($speed > 180):
$punishment = $punishment * 1.5;
else:
$punishment = $punishment * 0;
endif;
echo '车速=' . $speed . 'km/h:' . '处罚' . $punishment . '<br>';
echo '<hr>';
//4.switch 简化多分支
//车速
$speed = 190;
//罚款
$punishment = 200;
switch (true):
case $speed > 120 && $speed <= 140:
$punishment = $punishment * 1;
break;
case $speed > 140 && $speed <= 180;
$punishment = $punishment * 1.2;
break;
case $speed >180;
$punishment = $punishment * 1.5;
break;
default:
die('不罚款');
endswitch;
echo '车速=' . $speed . 'km/h:' . '处罚' . $punishment . '<br>';
echo '<hr>';
//二.循环举例
//建立数组
$names = ['马飞', '韩伟', '三民', '张佳林', '小孙'];
//1.while循环
while ($name = current($names)){
echo $name . '<br>';
next($names);
}
//指针复位
reset($names);
echo '<hr>';
//while改为模版语法
while ($name = current($names)):
echo $name. '<br>';
next($names);
endwhile;
echo '<hr>';
//2. 计数型
reset($names);
echo '数组仲有' . count($names);
echo '<hr>';
for ($i = 5; $i > -1; $i--){
echo $names[$i] . '<br>';
}
echo '<hr>';
//模版语法
reset($names);
for ($i = 0; $i < count($names); $i++):
echo $names[$i];
endfor;
echo '<hr>';
$names = ['马飞', '韩伟', null, '三民', '张佳林', '小孙'];
//continue 终止当前循环,提前进入下一轮循环
reset($names);
for ($i= 0; $i < count($names); $i++):
//跳过空数据
if (!$names[$i]) break;
echo $names[$i] . '<br>';
if ($names[$i] === '三民') continue;
endfor;
echo '<hr>';
//太空船运算符
//这里运算的结果如果是-1,说明左边小于右边,如果是1,说明左边大于右边,如果是0,说明两边相等
var_dump(30 <=> 20);
echo '<hr>';
//&&所有的条件必须同时满足返回是1,否则是0,可以理解成"和"
var_dump(3>2 && 5<40 && 8>2);
echo '<hr>';
//|| 条件当中只要有一个值为1,返回值就是true,可以理解成"或"
var_dump(3>20 || 5>8);
echo '<hr>';
$name1 = 'admin';
$name2 = 'chengdongxu';
if (@$name1 && $name2 === 'admin'){
echo '欢迎您';
}else {
echo '未登陆';
}
echo '<hr>';
//简化版双分支
if (@$name1 && $name2 === 'chdongxu') echo '有此人'; else echo '无此人';
echo '<hr>';
echo (@$name1 || $name2 === 'chengdongxu')? '有此人' : '无此人';
echo '<hr>';
//合并运算符
$nam3 = 'mafei';
$name3 = isset($name3) ? $name3 : 'root';
//使用合并运算符
$name3 = $name3 ?? 'cuowu';
echo $name3;