本文实例讲述了thinkPHP模板中for循环与switch语句用法。分享给大家供大家参考,具体如下:
1.for用法
1
2
|
< for start= "开始值" end = "结束值" comparison= "" step= "步进值" name= "循环变量名" >
</ for >
|
案例
1
2
3
|
< for start= "1" end = "100" >
{ $i }
</ for >
|
解析为
1
2
3
|
for ( $i =1; $i <100; $i +=1){
echo $i ;
}
|
2.switch用法
1
2
3
4
5
|
< switch name= "变量" >
< case value= "值1" break = "0或1" >输出内容1</ case >
< case value= "值2" >输出内容2</ case >
< default />默认情况
</ switch >
|
案例
1
2
3
4
5
|
< switch name= "User.level" >
< case value= "1" >value1</ case >
< case value= "2" >value2</ case >
< default /> default
</ switch >
|
也可以对case的value属性使用变量,例如:
1
2
3
4
5
|
< switch name= "User.userId" >
< case value= "$adminId" >admin</ case >
< case value= "$memberId" >member</ case >
< default /> default
</ switch >
|
综合案例
1
2
3
4
5
6
7
8
9
|
< for start= "0" end = "$credit['num']" >
< switch name= "credit['level']" >
< case value= "1" ><img src= "{sh::PUB}img/credit/level-heart2.png" class = "star" ></ case >
< case value= "2" ><img src= "{sh::PUB}img/credit/level-diamond2.png" class = "star" ></ case >
< case value= "3" ><img src= "{sh::PUB}img/credit/level-crown2.png" class = "star" ></ case >
< case value= "4" ><img src= "{sh::PUB}img/credit/level-super-crown2.png" class = "star" ></ case >
< default /> default
</ switch >
</ for >
|
希望本文所述对大家基于ThinkPHP框架的PHP程序设计有所帮助。