ThinkPHP models and operations use the same arithmetic operators as native PHP. The main problem is that ThinkPHP uses arithmetic operators in templates.
Use arithmetic operators and native PHP in ThinkPHP models and operations
Consistency. The main problem is that you need to pay attention to the use of arithmetic operators in templates.
ThinkPHP
The built-in template engine supports the use of arithmetic operators (+,-, *,/, and %) in the template. for example:
- Public function index (){
- $ X = 1;
- $ Y = 2;
- $ Z = 3;
- $ This-> assign ('X', $ x );
- $ This-> assign ('Y', $ y );
- $ This-> assign ('Z', $ z );
- }
In the corresponding template, output the results after performing mathematical operations on them:
{$ X + $ y}
{$ X + $ y * $ z}
The output results are as follows:
3 and 7.
Note:
When you use mathematical operators in a template, you cannot use the dot syntax or regular function usage. Therefore, if the query result is used, try to use the array syntax, such:
- // Incorrect usage
- {$ User. score + 10}
- // Correct usage
- {$ User ['score '] + 10}
- {$ User ['score '] * $ user ['level']}
- // Incorrect usage
- {$ User ['score '] | myFun * 10}
- // Correct usage
- {$ User ['score '] + myFun ($ user ['level'])}
In the above example, myFun is a user-defined function or PHP built-in function. For more information, see ThinkPHP
Use functions in the template.
Read more
PHP operators
ThinkPHP template variable output
ThinkPHP uses functions in the template