PHP template engine Smarty Generate random number smarty in math function usage

Source: Internet
Author: User
Tags mathematical functions php template smarty template
    1. {Math Equation=rand (125,324)}
Copy Code

What do you think? is not very simple, in fact, the main use of the Smarty template in the math function, the following smarty math function and usage to do a brief explanation. Math allows the template designer to perform mathematical expression operations in the template. A variable of any numeric type can be used in an expression, and the result is output at the location of the math tag. The variables used in the expression are passed as arguments to the function, which can be either template variables or static values. The currently available operators are: +,-,/, *, ABS, ceil, COS, exp, floor, log, log10, max, Min, pi, pow, rand, round, sin, sqrt, Srans, and Tan.

For more information about mathematical functions, see the PHP documentation.

If the special attribute "Assign" is specified, the output value of the function is assigned to the template variable specified by assign instead of the direct output. However, it is important to note that the math function is inefficient because of the use of the Eval () function of PHP. Doing math in PHP is more efficient, so do the math in PHP as much as possible and assign the results to template variables.

Two, Smarty Math function Demo

Example 1:

    1. {* $height =4, $width = 5 *}
    2. {Math equation= "x + y" x= $height y= $width}
Copy Code

Output results: 9

Example 2:

    1. {* $row _height = ten, $row _width = #col_div # = 2, assigned in template *}
    2. {Math equation= "Height * width/division"
    3. height= $row _height
    4. Width= $row _width
    5. division= #col_div #}
Copy Code

Output results: 100

Example 3:

    1. {Math equation= "((x + y)/z)" X=2 y=10 z=2}
Copy Code

Output results: 6

Example 4:

    1. {* You can supply a format parameter in sprintf format *}
    2. {Math equation= "x + y" x=4.4444 y=5.0000 format= "%.2f"}
Copy Code

Output results: 9.44

  • Related Article

    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.