smarty四则运算里面怎么用小括号?

qvbfndcwy 2012-04-26 02:23:37

<?php
require_once "include/smarty_config.php";
$a=1;
$b=2;
$c=7;
$smarty->assign("a",$a);
$smarty->assign("b",$b);
$smarty->assign("c",$c);
$smarty->display("t.tpl");
?>


a+b:<{$a+$b}><br />
a/c:<{$a/$c|string_format:"%.2f"}><br />
(a+b)/c:<{($a+$b)/$c|string_format:"%.2f"}><br />


以上是程序和模板。
要怎么做才可以实现(a+b)/c?
...全文
257 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
qvbfndcwy 2012-04-26
  • 打赏
  • 举报
回复
最后决定不用括号了。
xuzuning 2012-04-26
  • 打赏
  • 举报
回复
smarty中的四则运算
已有 567 次阅读2006-12-4 11:14 |个人分类:PHP
0
{* $height=4, $width=5 *}
{math equation="x + y" x=$height y=$width}
OUTPUT:
9
{* $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:
100
{* you can use parenthesis *}
{math equation="(( x + y ) / z )" x=2 y=10 z=2}
OUTPUT:
6
{* you can supply a format parameter in sprintf format *}
{math equation="x + y" x=4.4444 y=5.0000 format="%.2f"}
OUTPUT:
9.44
qvbfndcwy 2012-04-26
  • 打赏
  • 举报
回复
<?php
require_once "include/smarty_config.php";
$a=1;
$smarty->assign("a",$a);
$smarty->display("t.tpl");
?>

<{$a*100+$b*100+$c*100+$d*100}>


以上只定义了一个$a=1,传给smarty,但是模板里面的语法
<{$a*100+$a*100+$a*100+$a*100}>
确实会出错。

而改成:
<{$a*100+$a*100+$a*100}>
就正确了。
一起混吧 2012-04-26
  • 打赏
  • 举报
回复
加括号把,由于没有smarty环境。测试不了。看其他朋友怎么说。
xuzuning 2012-04-26
  • 打赏
  • 举报
回复
qvbfndcwy 2012-04-26
  • 打赏
  • 举报
回复
好吧,还是回到正题。
smarty进行四则运算,优先级怎么定义?
一起混吧 2012-04-26
  • 打赏
  • 举报
回复
提示什么错误? php完全可以把各结果得到了再传给smarty。不就是多几个变量或是数组的问题。
qvbfndcwy 2012-04-26
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]
你应该尽量在php中运算。再assign给模板。

另外。你的代码有什么错?
[/Quote]
第一行没错:a+b:<{$a+$b}><br />
第二行也没错:a/c:<{$a/$c|string_format:"%.2f"}><br />
第三行有错:(a+b)/c:<{($a+$b)/$c|string_format:"%.2f"}><br />
就是想问第三行要怎么改

另外,有十几列数据,要实现的是各列间的各种四则运算,php不可能把各结果都得到了再传给smarty吧。
一起混吧 2012-04-26
  • 打赏
  • 举报
回复
你应该尽量在php中运算。再assign给模板。

另外。你的代码有什么错?

21,886

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧