1.分支结构
1.1单分支
//1.单分支
$amount=5500;
//实际支付的金额,默认值是不打折的金额
$payment=$amount;
//if($amount>=5000){
////但流程控制语句中只有一行代码的时候,可以省略大括号
//$payment=$amount*0.9;
//}
if($amount>=5000):
//但流程控制语句中只有一行代码的时候,可以省略大括号
$payment=$amount*0.9;
endif;
echo’实际支付:’.$payment,'<br>’;
//模板语法:就是干掉流程控制中的大括号
//将起始大括号用冒号代替,将结束大括号用特殊关键字代替
echo'<hr>’;

分支结构运用多个实例和循环方式

1.2双分支
//2.双分支
if($amount>=5000){
$payment=$amount*0.9;
}else{
$payment=$amount;
}
//模板语法
$amount=2450;
if($amount>=5000):
$payment=$amount*0.9;
else:
$payment=$amount;
endif;
echo’实际支付:’.$payment,'<br>’;
echo'<hr>’;
1.3多分支
//3.多分支
//$amount=15555;
//if($amount>5000&&$amount<10000){
//$payment=$amount*0.9;
//}elseif($amount>=10000&&$amount<20000){
//$payment=$amount*0.7;
//}elseif($amount>=20000&&$amount<30000){
//$payment=$amount*0.5;
//}else{
//$payment=$amount;
//}
$amount=15555;
if($amount>5000&&$amount<10000):
$payment=$amount*0.9;
elseif($amount>=10000&&$amount<20000):
$payment=$amount*0.7;
elseif($amount>=20000&&$amount<30000):
$payment=$amount*0.5;
else:
$payment=$amount;
endif;
echo’实际支付:’.$payment,'<br>’;
echo'<hr>’;
//4.switch:用来简化多分支
//switch(表达式){
//case条件1:
//分支语句…
//break;
//case…
//default:
//…..
//}
$amount=4000;
switch(true){
case($amount>5000&&$amount<10000):
$payment=$amount*0.9;
break;
case($amount>=10000&&$amount<20000):
$payment=$amount*0.7;
break;
case($amount>=20000&&$amount<30000):
$payment=$amount*0.5;
break;
default:
$payment=$amount;
}
echo’实际支付:’.$payment,'<br>’;
echo'<hr>’;
//switch:通常用在单值判断中
$discount=0.8;
$amount=13500;
//switch($discount){
//case0.7:
//$payment=$amount*0.7;
//break;
//case0.8:
//$payment=$amount*0.8;
//break;
//case0.9:
//$payment=$amount*0.9;
//break;
//default:
//die(‘不支持的折扣率’);
//}
switch($discount):
case0.7:
$payment=$amount*0.7;
break;
case0.8:
$payment=$amount*0.8;
break;
case0.9:
$payment=$amount*0.9;
break;
default:
die(‘不支持的折扣率’);
endswitch;
echo’实际支付:’.$payment,'<br>’;
echo'<hr>’;
2.循环方式
2.1while
<?php
$cities=[‘合肥’,’长沙’,’成都’,’杭州’,’上海’,’深圳’];
//判断型循环:入口判断型while(条件){…}
//获取当前数组元素的值
//echocurrent($cities);
//next($cities);
//echocurrent($cities);
//next($cities);
//echocurrent($cities);
//next($cities);
//echocurrent($cities);
//next($cities);
//echocurrent($cities);
//next($cities);
//echocurrent($cities);
//next($cities);
//进入循环代码之前进行判断,只有条件为真才执行循环体
//while($city=current($cities)){
//echo$city,”<br>”;
//next($cities);
//}
//数组指针复位,重新指向第一个
//reset($cities);
//while($city=current($cities)){
//echo$city,”<br>”;
//next($cities);
//}
//模板语法
//reset($cities);
//while($city=current($cities)):
//echo$city,”<br>”;
//next($cities);
//endwhile;
//1.判断型循环,出口判断型do{…}while(条件)
//reset($cities);
//do{
//echo$city,”<br>”;
//next($cities);
//}while($city=current($cities));
//do-while没有对应的模板语法
?>
2.2for
//3.计数型循环:for(循环变量的初始化;循环条件;更新循环条件){…}
//遍历数组
echocount($cities),'<br>’;
//$i:数组元素的索引,默认从0开始
//for($i=0;$i<count($cities);$i++){
//echo$cities[$i],'<br>’;
//}
//模板语法
//for($i=0;$i<count($cities);$i++):
//echo$cities[$i],'<br>’;
//endfor;
for($i=0;$i<count($cities);$i++):
//输出拦截,break;提前终止循环
//if($i>3)break;
//有选择性的输出,跳过一部分元素
if($i===4)continue;
echo$cities[$i],'<br>’;
endfor;
3.模板语法二维数组实战运用
<?php
//数据查询结果通常是一个二维数组,用二维数组来模拟
$users=[
[‘id’=>1,’name’=>’zhu’,’grade’=>90],
[‘id’=>2,’name’=>’zhu1’,’grade’=>92],
[‘id’=>3,’name’=>’zhu2’,’grade’=>91],
[‘id’=>4,’name’=>’zhu3′,’grade’=>93],
];
?>
<!DOCTYPEhtml>
<htmllang=”en”>
<head>
<metacharset=”UTF-8″>
<metaname=”viewport”content=”width=device-width,initial-scale=1.0″>
<title>Document</title>
</head>
<body>
<!–<?php
$table.='<tableborder=”1″>’;
for($i=0;$i<count($users);$i++){
$table.='<tr>’;
$table.='<td>’.$users[$i][‘id’].'</td>’;
$table.='<td>’.$users[$i][‘name’].'</td>’;
$table.='<td>’.$users[$i][‘grade’].'</td>’;
$table.='</tr>’;
}
$table.='</table>’;
echo$table;
?>–>
<tableborder=”1″>
<?phpfor($i=0;$i<count($users);$i++):?>
<tr>
<td><?phpecho$users[$i][‘id’]?></td>
<td><?phpecho$users[$i][‘name’]?></td>
<td><?phpecho$users[$i][‘grade’]?></td>
</tr>
<?phpendfor;?>
</table>
</body>
</html>

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。