value1"/> value1">

Home  >  Article  >  Backend Development  >  Detailed explanation of if usage examples in ThinkPHP

Detailed explanation of if usage examples in ThinkPHP

伊谢尔伦
伊谢尔伦Original
2017-06-24 15:14:102295browse

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>

Note :Can support eq and other judgments in the condition attributeExpression is the same as the comparison tag above, but the usage with symbols such as ">", "<" is not supported because it will cause confusion Template parsing , so the following usage is wrong:

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

must be changed to:

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

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

<if condition="strtoupper($user[&#39;name&#39;]) neq &#39;THINKPHP&#39; "> ThinkPHP
<else /> other Framework
</if>

The condition attribute can support dot syntax and object syntax, for example, automatically determine whether the user variable is an array or an object:

<if condition="$user.name neq &#39;ThinkPHP&#39; "> ThinkPHP
<else /> other Framework
</if>

or know that the user variable is an object

<if condition="$user:name neq &#39;ThinkPHP&#39; "> ThinkPHP
<else /> other Framework
</if>

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, switch and comparison tags can be used The solution should be completed without if tags as much as possible. Because switch and comparison tags can use Variable Regulator and System Variable. If the IF tag still cannot meet certain special requirements, you can use native PHP code or PHP tags to directly write the code .

The above is the detailed content of Detailed explanation of if usage examples in ThinkPHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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