In this paper, we describe the method of thinkphp custom function to solve the template label addition and subtraction operation. Share to everyone for your reference. Specific as follows:
In the actual project, we often need the operation of the tag variable plus minus operation. However, in thinkphp, the operation of the direct operation of the template variable is not supported.
Fortunately, it provides a way to customize the function, and we can use the custom function to solve:
The thinkphp template Custom function syntax is as follows:
Format: {: function (...)} (refer to official Help document: http://thinkphp.cn/Manual/196)
Using this, let's try adding and subtracting.
First, define the function in thinkphp. Create a new common.php file under the project's Common folder (so the system will load automatically). Define two functions:
/** * Added 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));}
Second, use the function in the template:
Copy the Code Code as follows: {: Template_add ($var 1, $var 2)}
You can show the var1 of variables in var2.
It is important to note that if the variable is an array, it should be displayed as follows:
Copy the Code Code as follows: {: Template_add ($var [var1], $var [var2])}
Rather than we usually use dot grammar.
I hope this article is helpful to everyone based on thinkphp PHP program design.