This article mainly introduces how ThinkPHP user-defined functions can be used to add or subtract template tags. The example analyzes the usage skills of custom functions in ThinkPHP in template tags. For more information, see
This article mainly introduces how ThinkPHP user-defined functions can be used to add or subtract template tags. The example analyzes the usage skills of custom functions in ThinkPHP in template tags. For more information, see
This example describes how ThinkPHP UDF solves the addition and subtraction of template labels. Share it with you for your reference. The details are as follows:
In actual projects, we often need to add or subtract tag variables. However, in ThinkPHP, direct operations on Template variables are not supported.
Fortunately, it provides methods for user-defined functions. We can solve this problem using user-defined functions:
The syntax of the ThinkPHP template custom function is as follows:
Format: {: function (...)} (Refer to the official help documentation :)
Use this to try addition and subtraction.
1. define functions in ThinkPHP. Create a common. php file in the common folder of the project (the system will automatically load the file ). Define two functions:
/*** Add for template use * @ param $ A * @ param $ B */function template_add ($ a, $ B) {echo (intval ($ a) + intval ($ B);}/*** subtract, for template use * @ param $ A * @ param $ B */function template_substract ($ a, $ B) {echo (intval ($ a)-intval ($ B ));}
2. Use functions in the template:
The Code is as follows:
{: Template_add ($ var1, $ var2 )}
The sum of the variable var1 and var2.
Note that if the variable is an array, it should be displayed as follows:
The Code is as follows:
{: Template_add ($ var [var1], $ var [var2])}
Instead, we usually use the dot syntax.
I hope this article will help you with ThinkPHP-based php programming.