当前位置:Gxlcms > PHP教程 > phpSmarty中if,elseif,else用法详解

phpSmarty中if,elseif,else用法详解

时间:2021-07-01 10:21:17 帮助过:49人阅读

php Smarty模板条件选择结构if-elseif-else,{if}必须和{/if}成对出现,当然也可以使用{else}和{elseif}子句,{if}中可以使用如下修饰词:

if实例

{if} 实例:

  1. {if $name eq 'Fred'}
  2. Welcome Sir.
  3. {elseif $name eq 'Wilma'}
  4. Welcome Ma'am.
  5. {else}
  6. Welcome, whatever you are.
  7. {/if}
  8. {* an example with "or" logic *}
  9. {if $name eq 'Fred' or $name eq 'Wilma'}
  10. ...
  11. {/if}
  12. {* same as above *}
  13. {if $name == 'Fred' || $name == 'Wilma'}
  14. ...
  15. {/if}
  16. {* parenthesis are allowed *}
  17. {if ( $amount < 0 or $amount > 1000 ) and $volume >= #minVolAmt#}
  18. ...
  19. {/if}
  20. {* you can also embed php function calls *}
  21. {if count($var) gt 0}
  22. ...
  23. {/if}
  24. {* check for array. *}
  25. {if is_array($foo) }
  26. .....
  27. {/if}
  28. {* check for not null. *}
  29. {if isset($foo) }
  30. .....
  31. {/if}
  32. {* test if values are even or odd *}
  33. {if $var is even}
  34. ...
  35. {/if}
  36. {if $var is odd}
  37. ...
  38. {/if}
  39. {if $var is not odd}
  40. ...
  41. {/if}
  42. {* test if var is divisible by 4 *}
  43. {if $var is div by 4}
  44. ...
  45. {/if}
  46. {*
  47. test if var is even, grouped by two. i.e.,
  48. 0=even, 1=even, 2=odd, 3=odd, 4=even, 5=even, etc.
  49. *}
  50. {if $var is even by 2}
  51. ...
  52. {/if}
  53. {* 0=even, 1=even, 2=even, 3=odd, 4=odd, 5=odd, etc. *}
  54. {if $var is even by 3}
  55. ...
  56. {/if}

if elseif 实例:

  1. {if isset($name) && $name == 'Blog'}
  2. {* do something *}
  3. {elseif $name == $foo}
  4. {* do something *}
  5. {/if}
  6. {if is_array($foo) && count($foo) > 0}
  7. {* do a foreach loop *}
  8. {/if}

实例

  1. <table>
  2. {foreach from=$users item=row}
  3. <tr>
  4. {foreach from=$row item=col key=k}
  5. {* 注意if语句与小括号(不是必须的)之间的空格,有严格的要求 *}
  6. <td {if ($col == 'wjj') }style='color:red;'{elseif ($col == 'qxy')}style='color:green;'{/if}>{$k}:{$col}</td>
  7. {/foreach}
  8. </tr>
  9. {foreachelse}
  10. <tr><td>没有数据啊</td></tr>
  11. {/foreach}
  12. </table>

程序为$users变量赋值为如下数组:

  1. array(
  2. array('name' => 'wjj','age' => '保密'),
  3. array('name' => 'qxy','age' => '好像比我小')
  4. )

以上就是php Smarty中if,elseif,else用法详解的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行