thinkphp custom function to solve template label addition and subtraction operation _php Example

Source: Internet
Author: User

The example in this article describes the method of thinkphp custom function to solve template label addition and subtraction operations. Share to everyone for your reference. Specifically as follows:

In the actual project, we often need to add and subtract the label variable 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 that we can use to solve the custom function:

The thinkphp template Custom function syntax is as follows:

Format: {: function (...)} (refer to official Help document: http://thinkphp.cn/Manual/196)
Using this, we try to add and subtract.

First, define the function in the thinkphp. Create a new common.php file under the project's Common folder (so that the system will automatically load). Define two functions:

/** 
 * Add for template use 
 * @param <type> $a 
 * @param <type> $b 
/function Template_add ($a, $b) { C6/>echo (Intval ($a) +intval ($b)); 
} 
/** 
 * Subtract for template use 
 * @param <type> $a 
 * @param <type> $b/ 
function Template_ Substract ($a, $b) { 
  echo (intval ($a)-intval ($b)); 
} 

Use functions in the template:

Copy Code code as follows:
{: Template_add ($var 1, $var 2)}

You can show the variable var1 to the var2.
Note that if the variable is an array, you want to display this:
Copy Code code as follows:
{: Template_add ($var [var1], $var [var2])}

Instead of us, we usually use dot syntax.

I hope this article will help you with the PHP program design based on thinkphp.

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.