Detailed explanation of IF tag usage in ThinkPHP template_PHP tutorial

WBOY
Release: 2016-07-13 10:26:23
Original
717 people have browsed it

ThinkPHP’s IF tag can be used to define complex conditional judgments , for example:

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

Note: The condition attribute can support judgment expressions such as eq, which are the same as the comparison tags above, but usage with symbols such as ">" and "<" is not supported because it will confuse template parsing. , so the following usage is incorrect:

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

must be changed to:

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

In addition, we can use php code in the condition attribute, for example:

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

The condition attribute can support dot syntax and object syntax, such as automatically determining whether a user variable is an array or an object:

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

Or know that the user variable is an object

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

Note: Since the condition attribute of the if tag basically uses PHP syntax, it will be more concise to use judgment tags and switch tags as much as possible. In principle, if it can be solved with switch and comparison tags, try not to use if Label completed. Because switch and comparison tags can use variable modifiers and system variables. If the IF tag still cannot meet certain special requirements, you can use native PHP code or PHP tags to write code directly .

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/824711.htmlTechArticleThinkPHP’s IF tag can be used to define complex conditional judgments, for example: if condition="($name eq 1 ) OR ($name gt 100) " value1elseif condition="$name eq 2" /value2else / value3/if Note...
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