Detailed explanation of if usage examples in ThinkPHP

伊谢尔伦
Release: 2023-03-11 10:54:02
Original
2354 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 :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>
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[&#39;name&#39;]) neq &#39;THINKPHP&#39; "> ThinkPHP
<else /> other Framework
</if>
Copy after login

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>
Copy after login

or know that the user variable is an object

<if condition="$user:name neq &#39;ThinkPHP&#39; "> 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, 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!

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