Home  >  Article  >  Backend Development  >  Detailed explanation of the method and math function for generating random numbers in PHP template engine smarty_PHP Tutorial

Detailed explanation of the method and math function for generating random numbers in PHP template engine smarty_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 10:31:55777browse

Of course this is definitely feasible, but it is too complicated. The author below will share with you a little trick on how to generate random numbers directly in smarty templates.

Now suppose you need to generate a random number between 125-324 in the smarty template, then you can write it as follows:

Copy the code The code is as follows:

{math equation=rand(125,324)}

This has achieved our goal, how about it? 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. The variables used in the expression are passed to the function as parameters, Can be a template variable or a 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 details on the 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. Doing mathematical operations in PHP will be more efficient, so do mathematical operations in PHP as much as possible. Assign the result to a template variable.

smarty math function demonstration

Copy code The code is as follows:

{* $height=4, $width=5 *}

{math equation="x + y" x=$height y=$width}


Output result: 9
Copy code The code is as follows:

{* $row_height = 10, $row_width = 20, #col_div# = 2, assigned in template *}

{math equation="height * width / division"
height=$row_height
width=$row_width
division=#col_div#}


Output result: 100
Copy code The code is as follows:

{math equation="(( x + y ) / z )" x=2 y=10 z=2}


Output result: 6
Copy code The code is as follows:

{* you can supply a format parameter in sprintf format *}

{math equation="x + y" x=4.4444 y=5.0000 format="%.2f"}


Output result: 9.44

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/760283.htmlTechArticleOf course this is definitely feasible, but it is too complicated. The author below explains how to generate random numbers directly in the smarty template. Let me share some tips with you. Now suppose that in the smarty template you need...
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