Home > Backend Development > PHP Tutorial > ThinkPHP模板IF标签用法详解_php实例

ThinkPHP模板IF标签用法详解_php实例

WBOY
Release: 2016-06-07 17:17:58
Original
901 people have browsed it

ThinkPHP的IF标签可以用来定义复杂的条件判断,例如:

<if condition="($name eq 1) OR ($name gt 100) "> value1
<elseif condition="$name eq 2" />value2
<else /> value3
</if>
Copy after login

注意:在condition属性中可以支持eq等判断表达式 ,同上面的比较标签,但是不支持带有”>”、”<”等符号的用法,因为会混淆模板解析,所以下面的用法是错误的:

<if condition="$id < 5 "> value1
<else /> value2
</if>
Copy after login

必须改成:

<if condition="$id lt 5 "> value1
<else /> value2
</if>
Copy after login

除此之外,我们可以在condition属性里面使用php代码,例如:

<if condition="strtoupper($user['name']) neq 'THINKPHP' "> ThinkPHP
<else /> other Framework
</if>
Copy after login

condition属性可以支持点语法和对象语法,例如自动判断user变量是数组还是对象:

<if condition="$user.name neq 'ThinkPHP' "> ThinkPHP
<else /> other Framework
</if>
Copy after login

或者知道user变量是对象

<if condition="$user:name neq 'ThinkPHP' "> ThinkPHP
<else /> other Framework
</if>
Copy after login

注意:由于if标签的condition属性里面基本上使用的是php语法,尽可能使用判断标签和Switch标签会更加简洁,原则上来说,能够用switch和比较标签解决的尽量不用if标签完成。因为switch和比较标签可以使用变量调节器和系统变量。如果某些特殊的要求下面,IF标签仍然无法满足要求的话,可以使用原生php代码或者PHP标签来直接书写代码

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template