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 official help documentation: http://thinkphp.cn/Manual/196)
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 the template to use * @ Param <type> $ A * @ Param <type> $ B * @ Author: liufangfang.net@gmail.com */function template_add ($, $ B) {echo (intval ($ A) + intval ($ B) ;}/ *** subtract, for template use * @ Param <type> $ A * @ Param <type> $ B * @ Author: liufangfang.net@gmail.com */function template_substract ($ A, $ B) {echo (intval ($ A)-intval ($ B ));}
2. Use functions in the template:
{: 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:
{: Template_add ($ var [var1], $ var [var2])}
Instead, we usually use the dot syntax.