php字符串在不应该[关闭]时回显为0
问题描述:
I'm writing code to show the prices of some products. If the price is 0 it doesn't show anything.
<? 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";}?>
This echoes "€0.00 £33.00 $66.00 ". The € price is set to 99. I can see no reason whatsoever that this should echo as 0! Am I doing something wrong? Bad syntax?
我正在编写代码来显示某些产品的价格。 如果价格为0,则不显示任何内容。 p>
&lt;? if($ book-&gt; price_euro!= 0){$ europrice = number_format($ book-&gt; price_eur,2); echo“€$ europrice”;}?&gt;
&lt;? if($ book-&gt; price_gbp!= 0){$ gbpprice = number_format($ book-&gt; price_gbp,2); echo“£$ gbpprice”;}?&gt;
&lt;? if($ book-&gt; price_usd!= 0){$ usdprice = number_format($ book-&gt; price_usd,2); echo“$$ usdprice”;}?&gt;
code> pre>
这回响“€0.00£33.00 $ 66.00”。 €价格设置为99.我认为没有任何理由认为这应该为0! 难道我做错了什么? 语法错误? p>
div>
答
$europrice = number_format($book->price_eur, 2
shouldn't it be $book->price_euro
?
答
Typo in Euro
number_format($book->price_eur, 2); // This is what you have.
number_format($book->price_euro, 2); // This is what you need.
答
I don't see anything wrong with this code without seeing more. But maybe your variable?
<? if ($book->price_euro != 0) {$europrice = number_format($book->price_eur, 2); echo "€$europrice";}?>
price_eur
should be price_euro
?
答
You might still be getting null values or something else try using empty instead
<? 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";}?>
答
number_format($book->price_eur, 2);
should be
number_format($book->price_euro, 2);
The null value is causing your problem.