Home >Backend Development >PHP Tutorial >Detailed explanation of IF tag usage in ThinkPHP template_PHP tutorial
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: 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>
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['name']) neq 'THINKPHP' "> ThinkPHP <else /> other Framework </if>
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>
Or know that the user variable is an object
<if condition="$user:name neq 'ThinkPHP' "> 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, 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 .