Blogger Information
Blog 1
fans 0
comment 0
visits 554
Related recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
php的分支结构及循环知识总结
开惢鷲好
Original
554 people have browsed it

分支结构

四种不同的方式判断了price数值所在不同的区间。(注:模板语法书写时endif 或 endswitch 后面要加; 否则报错)

<?php
$price=22530;
echo $price;
echo "<hr>";

if ($price>0 && $price<10000){
         echo "1万以下";
}elseif($price>=10000 && $price<15000){
       echo "1-1.5万";
}elseif($price>=15000 && $price<20000){
       echo "1.5-2万";
}elseif($price>=20000 && $price<=25000){
       echo "2-2.5万";
}else{
      echo "负数或大于2万";
}
echo "<hr>";

//改写成模板语法
if ($price>0 && $price<10000):
       echo "1万以下";
elseif($price>=10000 && $price<15000):
       echo "1-1.5万";
elseif($price>=15000 && $price<20000):
       echo "1.5-2万";
elseif($price>=20000 && $price<=25000):
       echo "2-2.5万";
else:
      echo "负数或大于2万";
endif;
echo "<hr>";

//switch 实现
 switch (true) {
     case $price>0 && $price<10000:
        echo '1万以下';
        break;
     case $price>=10000 && $price<15000:
        echo '1-1.5万';
        break;
     case $price>=15000 && $price<=20000:
        echo '1.5-2万';
        break;
     case $price>=20000 && $price<=25000:
        echo '2-2.5万';
        break;
    default:
        echo '负数或大于2万';
}
echo "<hr>";
//改写成模板语法
 switch (true) :
     case $price>0 && $price<10000:
        echo '1万以下';
        break;
     case $price>=10000 && $price<15000:
        echo '1-1.5万';
        break;
     case $price>=15000 && $price<=20000:
        echo '1.5-2万';
        break;
     case $price>=20000 && $price<=25000:
        echo '2-2.5万';
        break;
    default:
        echo '负数或大于2万';
 endswitch;
?>

循环知识

1、一维数组

用for 循环一维数组最常用,也可以用第二种方式while 来循环读出,可以改模板语法,同样在endfor与endwhile 后面要加; 号(注:do while不能用模板语法)

 $cities = ['合肥', '南京', '杭州', '苏州', '上海'];
 for ($i=0;$i<count($cities);$i++){
      echo $cities[$i]."<br>";
 }

2、二维数组

用for 与 while 循环都可以输出二维数组,同样可以改成模板语法,方法与前面的相同 (注:do while 做循环输出二维数组时,第一条记录会报错或忽略)

 $users = [
    ['id'=>1, 'name'=>'zhu', 'grade'=> 60],
    ['id'=>2, 'name'=>'admin', 'grade'=> 50],
    ['id'=>3, 'name'=>'peter', 'grade'=> 20],
];

//for 循环

for ($i=0;$i<count($users);$i++){
    echo $users[$i]['id'].$users[$i]['name'].$users[$i]['grade']."<br>";
}

//while 循环

while ($city = current($users)){
     echo $city['id'].$city['name'].$city['grade'], '<br>';
     next($users);
}

Correcting teacher:GuanhuiGuanhui

Correction status:qualified

Teacher's comments:要认真完成作业,代码展示,要使用代码块。
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post