Home  >  Article  >  Backend Development  >  PHP template engine smarty generates random numbers Usage of math function in smarty

PHP template engine smarty generates random numbers Usage of math function in smarty

WBOY
WBOYOriginal
2016-07-25 08:53:291137browse
  1. {math equation=rand(125,324)}
How about copying the code

? Isn’t it very simple? In fact, the math function in the smarty template is mainly used here. Here is a brief explanation of the function and usage of the Smarty math function. math allows template designers to perform mathematical expression operations in templates. Variables of any numeric type can be used in expressions, and the results are output in the position of the math tag. Variables used in expressions are passed as parameters to functions, which can be templates Variable or static value. 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 on math functions, check out the PHP documentation.

If the special attribute "assign" is specified, the output value of the function will be assigned to the template variable specified by assign instead of being output directly. However, it should be noted that: due to the use of PHP's eval() function, the execution efficiency of the math function is not high. It is more efficient to perform mathematical operations in PHP, so it is necessary to perform mathematical operations in PHP as much as possible and assign the results to to template variables.

Second, smart math function demonstration

Example 1:

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

Output result: 9

Example 2:

  1. {* $row_height = 10, $row_width = 20, #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 result: 100

Example 3:

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

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"}
Copy code

Output result: 9.44



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn