如何更正我的php number_format错误?

时间:2014-04-22 00:38:18

标签: php arrays

我的数组代码出现了以下错误。如何修复此错误以显示结果?

错误:

Warning: number_format() expects parameter 1 to be double, string

我的代码:

$account['checking'] = 775.57;
$account['savings'] = 2275.31;
$account['moneyMarket'] = 4554.43;

print("<table>");
print("<tr><td>CHECKING BALANCE:  </td><td>$".number_format(checking, 2)." </td></tr>");     
print("<tr><td>SAVINGS BALANCE:   </td><td>$".number_format(savings, 2)."</td></tr>");
print("<tr><td>MONEY MKT BALANCE: </td><td>$".number_format(moneyMarket, 2)."</td></tr>");
print("</table>");

3 个答案:

答案 0 :(得分:1)

您需要使用$来访问变量......

number_format($account['checking'], 2)

否则您正在检查可能不存在的常量。

答案 1 :(得分:1)

你不应该:

print("<tr><td>CHECKING BALANCE:  </td><td>$".number_format($account['checking'], 2)." </td></tr>");     
print("<tr><td>SAVINGS BALANCE:   </td><td>$".number_format($account['savings'], 2)."</td></tr>");
print("<tr><td>MONEY MKT BALANCE: </td><td>$".number_format($account['moneyMarket'], 2)."</td></tr>");

而不是:

print("<tr><td>CHECKING BALANCE:  </td><td>$".number_format(checking, 2)." </td></tr>");     
print("<tr><td>SAVINGS BALANCE:   </td><td>$".number_format(savings, 2)."</td></tr>");
print("<tr><td>MONEY MKT BALANCE: </td><td>$".number_format(moneyMarket, 2)."</td></tr>");

答案 2 :(得分:1)

您无法正确访问阵列。 number_format(checking, 2)应为number_format($account['checking'], 2)