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

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

coldplay.xixi
coldplay.xixiforward
2020-07-24 17:24:532256browse

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

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 the smarty template.

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

The code is as follows:

{math equation=rand(125,324)}

This way We have achieved our goal, how about it? Isn’t it very simple? In fact, the math function in the smarty template is mainly used here. The following 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 Given to a function, it 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 mathematical 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 it in PHP as much as possible Mathematical operations, assigning results to template variables.

smarty math Function demonstration

The code is as follows:

{* $height=4, $width=5 *}
{math equation="x + y" x=$height y=$width}

Output result: 9

The code is as follows :

{* $row_height = 10, $row_width = 20, #col_p# = 2, assigned in template *}
{math equation="height * width / pision" height=$row_height width=$row_width pision=#col_p#}

Output result: 100

The code is as follows:

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

Output result: 6

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

Related learning recommendations: PHP programming from entry to proficiency

The above is the detailed content of Detailed explanation of the method and math function for generating random numbers in PHP template engine smarty. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:jb51.net. If there is any infringement, please contact admin@php.cn delete