PHP模板引擎smarty產生隨機數 smarty中math函數用法

來源:互聯網
上載者:User
  1. {math equation=rand(125,324)}
複製代碼

怎麼樣?是不是很簡單,其實這裡主要用到了smarty模板中的math函數,下面對Smarty math函數的作用以及用法做個簡要的講解。math 允許模板設計者在模板中進行數學運算式運算. 均運算式中可以使用任何數實值型別的變數,結果在 math 標籤的位置輸出. 運算式中使用的變數被當做參數傳遞給函數,可以是模板變數或靜態值. 目前可以使用的運算子有:+, -, /, *, abs, ceil, cos, exp, floor, log, log10, max, min, pi, pow, rand, round, sin, sqrt, srans 和 tan .

關於數學函數的詳細資料,請查看 PHP 文檔。

如果指定了 "assign" 這個特殊屬性,該函數的輸出值將被賦給由 assign 指定的模板變數,而不是直接輸出。不過需要注意的是: 由於使用了 php 的 eval() 函數,math 函數的執行效率不高. 在 PHP 中做數學運算效率會更高一些,因此要儘可能在 PHP 中做數學運算,將結果賦給模板變數。

二,smarty math 函數示範

例1:

  1. {* $height=4, $width=5 *}
  2. {math equation="x + y" x=$height y=$width}
複製代碼

輸出結果:9

例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#}
複製代碼

輸出結果:100

例3:

  1. {math equation="(( x + y ) / z )" x=2 y=10 z=2}
複製代碼

輸出結果:6

例4:

  1. {* you can supply a format parameter in sprintf format *}
  2. {math equation="x + y" x=4.4444 y=5.0000 format="%.2f"}
複製代碼

輸出結果:9.44

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.