The method of generating random numbers in PHP template engine smarty and the math function detailed _php template

Source: Internet
Author: User
Tags php template rand smarty template

This is certainly feasible, but too complex, the following author on how to directly in the Smarty template to generate random numbers this little trick to share with you.

Now suppose you need to generate a random number between 125-324 in the Smarty template, you can do the following:

Copy Code code as follows:

{Math Equation=rand (125,324)}

That's what we're going to do. is not very simple, in fact, this is mainly used in the Smarty template of the math function, the following on the role of Smarty math function and the use of a brief explanation.

The 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 position of the math label. Variables used in expressions are passed as arguments to functions, either template variables or static values. Currently available operators are: +,-,/, *, ABS, ceil, COS, exp, floor, log, log10, max, Min, pi, pow, rand, round, sin, sqrt, Srans and tan. About For more information about math functions, check the PHP documentation.

If the special attribute of "Assign" is specified, the output value of the function is assigned to the template variable specified by assign instead of direct output.

Note, however, that the math function is inefficient because of the eval () function of PHP. Doing math in PHP is more efficient, so try to do math in PHP as much as possible and assign the result to a template variable.

Smarty Math Function Demo

Copy Code code as follows:

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

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


Output Result: 9
Copy Code code as follows:

{* $row _height = ten, $row _width = #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 code as follows:

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


Output Result: 6
Copy Code code 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

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.