W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗(yàn)值獎勵
Smarty的{if}
條件判斷和PHP的if 非常相似,只是增加了一些特性。 每個{if}
必須有一個配對的{/if}
. 也可以使用{else}
和 {elseif}
. 全部的PHP條件表達(dá)式和函數(shù)都可以在if內(nèi)使用,如||, or, &&, and, is_array(), 等等.
如果開啟了安全機(jī)制,那么只有在$php_functions
設(shè)置允許的PHP函數(shù)才能使用。 參見安全機(jī)制。
下面是可用的運(yùn)算符列表,使用中都會放到元素的中間并且用空格分隔。 注意列表中[方括號]的是可選的,而且還會列出對應(yīng)PHP的表達(dá)式。
運(yùn)算符 | 別名 | 語法示例 | 含義 | 對應(yīng)PHP語法 |
---|---|---|---|---|
== | eq | $a eq $b | 等于 | == |
!= | ne, neq | $a neq $b | 不等于 | != |
> | gt | $a gt $b | 大于 | > |
< | lt | $a lt $b | 小于 | < |
>= | gte, ge | $a ge $b | 大于等于 | >= |
<= | lte, le | $a le $b | 小于等于 | <= |
=== | $a === 0 | 絕對等于 | === | |
! | not | not $a | 非 (一元運(yùn)算) | ! |
% | mod | $a mod $b | 取模 | % |
is [not] div by | $a is not div by 4 | 取模為0 | $a % $b == 0 | |
is [not] even | $a is not even | [非] 取模為0 (一元運(yùn)算) | $a % 2 == 0 | |
is [not] even by | $a is not even by $b | 水平分組 [非] 平均 | ($a / $b) % 2 == 0 | |
is [not] odd | $a is not odd | [非] 奇數(shù) (一元運(yùn)算) | $a % 2 != 0 | |
is [not] odd by | $a is not odd by $b | [非] 奇數(shù)分組 | ($a / $b) % 2 != 0 |
Example 7.44. {if} 表達(dá)式
{if $name eq 'Fred'} Welcome Sir. {elseif $name eq 'Wilma'} Welcome Ma'am. {else} Welcome, whatever you are. {/if} {* an example with "or" logic *} {if $name eq 'Fred' or $name eq 'Wilma'} ... {/if} {* same as above *} {if $name == 'Fred' || $name == 'Wilma'} ... {/if} {* parenthesis are allowed *} {if ( $amount < 0 or $amount > 1000 ) and $volume >= #minVolAmt#} ... {/if} {* you can also embed php function calls *} {if count($var) gt 0} ... {/if} {* check for array. *} {if is_array($foo) } ..... {/if} {* check for not null. *} {if isset($foo) } ..... {/if} {* test if values are even or odd *} {if $var is even} ... {/if} {if $var is odd} ... {/if} {if $var is not odd} ... {/if} {* test if var is divisible by 4 *} {if $var is div by 4} ... {/if} {* test if var is even, grouped by two. i.e., 0=even, 1=even, 2=odd, 3=odd, 4=even, 5=even, etc. *} {if $var is even by 2} ... {/if} {* 0=even, 1=even, 2=even, 3=odd, 4=odd, 5=odd, etc. *} {if $var is even by 3} ... {/if}
Example 7.45. {if} 的更多例子
{if isset($name) && $name == 'Blog'} {* do something *} {elseif $name == $foo} {* do something *} {/if} {if is_array($foo) && count($foo) > 0} {* do a foreach loop *} {/if}
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: