Use arithmetic operators in the ThinkPHP Template

Source: Internet
Author: User
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:




  1. Public function index (){
  2. $ X = 1;
  3. $ Y = 2;
  4. $ Z = 3;
  5. $ This-> assign ('X', $ x );
  6. $ This-> assign ('Y', $ y );
  7. $ This-> assign ('Z', $ z );
  8. }

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:




  1. // Incorrect usage
  2. {$ User. score + 10}
  3. // Correct usage
  4. {$ User ['score '] + 10}
  5. {$ User ['score '] * $ user ['level']}
  6. // Incorrect usage
  7. {$ User ['score '] | myFun * 10}
  8. // Correct usage
  9. {$ 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.