Usage of the math function in the PHP template engine smarty to generate a random number smarty

Source: Internet
Author: User
Tags smarty template
Usage of the math function in the PHP template engine smarty to generate a random number smarty

  1. {Math equation = rand (125,324 )}

How is it? Isn't it easy? in fact, math function in the smarty template is mainly used here. let's briefly explain the role and usage of the Smarty math function. Math allows the template designer to perform mathematical expression operations in the template. any numeric type variable can be used in the expression, and the result is output at the position of the math tag. the variables used in the expression are passed to the function as parameters. they can be template variables or static values. currently, the following operators can be used: +,-,/, *, 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 this function will be assigned to the template variable specified by assign, rather than directly output. However, it should be noted that the math function is inefficient to execute because php's eval () function is used. it is more efficient to perform mathematical operations in PHP. Therefore, you should perform mathematical operations in PHP as much as possible and assign the results to template variables.

2. demonstration of the smarty math function

Example 1:

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

Output result: 9

Example 2:

  1. {* $ Row_height = 10, $ row_width = 20, # col_p # = 2, assigned in template *}
  2. {Math equation = "height * width/pision"
  3. Height = $ row_height
  4. Width = $ row_width
  5. Pision = # col_p #}

Output result: 100

Example 3:

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

Output result: 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 "}

Output result: 9.44

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.