0

我正在编写代码来显示某些产品的价格。如果价格为 0,则不显示任何内容。

<? if ($book->price_euro != 0) {$europrice = number_format($book->price_eur, 2); echo "€$europrice";}?>
<? if ($book->price_gbp != 0) {$gbpprice = number_format($book->price_gbp, 2); echo "£$gbpprice";}?>
<? if ($book->price_usd != 0) {$usdprice = number_format($book->price_usd, 2); echo "$$usdprice";}?>

这与“€0.00 £33.00 $66.00”相呼应。欧元价格设置为 99。我看不出有任何理由应该回显为 0!难道我做错了什么?语法不好?

4

5 回答 5

10
$europrice = number_format($book->price_eur, 2

不应该$book->price_euro吗?

于 2012-08-30T15:29:54.443 回答
6

欧元的错别字

number_format($book->price_eur, 2); // This is what you have.

number_format($book->price_euro, 2); // This is what you need.
于 2012-08-30T15:30:08.540 回答
3

如果没有看到更多,我看不出这段代码有什么问题。但也许你的变量?

<? if ($book->price_euro != 0) {$europrice = number_format($book->price_eur, 2); echo "€$europrice";}?>

price_eur应该是price_euro

于 2012-08-30T15:30:19.490 回答
1
number_format($book->price_eur, 2);

应该

number_format($book->price_euro, 2);

空值导致您的问题。

于 2012-08-30T15:32:49.533 回答
0

您可能仍会得到空值或其他尝试使用空的东西

<? if (empty($book->price_euro)) {$europrice = number_format($book->price_euro, 2); echo "€$europrice";}?>
<? if (empty($book->price_gbp)) {$gbpprice = number_format($book->price_gbp, 2); echo "£$gbpprice";}?>
<? if (empty($book->price_usd)) {$usdprice = number_format($book->price_usd, 2); echo "$$usdprice";}?>
于 2012-08-30T15:31:03.490 回答