---
title: "smarty if 逻辑运算符"
author: "varsoft"
date: "2011-08-28T08:42:44Z"
tags: []
categories: ["编程语言"]
slug: "smarty-if"
draft: false
lastmod: "2011-08-28T08:42:44Z"
id: 1159645
url: "https://varsoft.iteye.com/blog/1159645"
---

Smarty 中的 if 语句和 php 中的 if 语句一样灵活易用，并增加了几个特性以适宜模板引擎. if 必须于 /if 成对出现. 可以使用 else 和 elseif 子句. 可以使用以下条件修饰词：eq、ne、neq、gt、lt、lte、le、gte、ge、is even、is odd、is not even、is not odd、not、mod、div by、even by、odd by、==、!=、&gt;、&lt;、&lt;=、&gt;=. 使用这些修饰词时必须和变量或常量用空格格开.

 

eq相等，

ne、neq不相等，

gt大于，

lt小于，

gte、ge大于等于，

lte、le 小于等于，

not非， mod求模。

is \[not] div by是否能被某数整除，

is \[not] even是否为偶数

$a is \[not] even by $b即($a / $b) % 2 == 0，

is \[not] odd是否为奇，

$a is not odd by $b即($a / $b) % 2 != 0 示例：

 

equal/ not equal/ greater than/ less than/ less than or equal/ great than or equal/后面的就不用说了

 

Smarty 中的 if 语句和php中的 if 语句一样灵活易用，并增加了几个特性以适宜模板引擎. if 必须于 /if 成对出现. 可以使用 else 和 elseif 子句. 可以使用以下条件修饰词：eq、ne、neq、gt、lt、lte、le、gte、ge、is even、is odd、is not even、is not odd、not、mod、div by、even by、odd by、==、!=、&gt;、&lt;、&lt;=、&gt;=. 使用这些修饰词时必须和变量或常量用空格格开.

 

Example 7-11. if statements

例 7-11. if 语句演示 

 

{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}

 

{* the following syntax will NOT work, conditional qualifiers

must be separated from surrounding elements by spaces \*}

{if $name=="Fred" || $name=="Wilma"}

...

{/if}

 

 

{* parenthesis are allowed \*}

{if ( $amount &lt; 0 or $amount &gt; 1000 ) and $volume &gt;= #minVolAmt#}

...

{/if}

 

{* you can also embed php function calls \*}

{if count($var) gt 0}

...

{/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}

 

 

 

{if $\_global\_password} 是判断变量存不存在....

 

{if $\_global\_password!==''} 是判断变量等不等于空....

 

Smarty 中的 if 语句和 php 中的 if 语句一样灵活易用，并增加了几个特性以适宜模板引擎. if 必须于 /if 成对出现. 可以使用 else 和 elseif 子句. 可以使用以下条件修饰词：eq、ne、neq、gt、lt、lte、le、gte、ge、is even、is odd、is not even、is not odd、not、mod、div by、even by、odd by、==、!=、&gt;、&lt;、&lt;=、&gt;=. 使用这些修饰词时必须和变量或常量用空格格开.

 

 

equal : 相等、not equal：不等于、greater than：大于、less than：小于、less than or equal：小于等于、great than or equal：大于等于、is even：是偶数、is odd：是奇数、is not even：不是偶数、is not odd：不是奇数、not：非、mod：取余、div by：被。。。除

 

 

下面是一段除法的样式：

&lt;{if $count is div by 4}&gt;

&lt;/tr&gt;&lt;tr&gt;

&lt;{/if}&gt;

这是一个表格中增加一行的方法，意思是有4列，如果够四列就换新行，具体的以后会讲到。