thinkphp a custom function to solve the template label addition and subtraction operation _php instance

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

  • 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.