如果变量为空,则为PHP echo text

时间:2010-05-23 20:45:41

标签: php variables echo

如果我的变量$ amount等于零,我试图回复€00.00

我做错了你可以帮忙吗??

代码

while ($row9 = mysql_fetch_array($result9))
{
$amount = $row9['amount'];
}
//$amount = $amount / 60;
//$amount = round($amount, 2);
if $amount == 0 echo "<b>Balance: &#8364;00.00</b>";
else
echo "<b>Balance: $$amount</b>";

3 个答案:

答案 0 :(得分:3)

你需要将if / else放在循环中,并且你有一些无效的语法(缺少parens和double $)。所以:

while ($row9 = mysql_fetch_array($result9))
{
  $amount = $row9['amount'];
  if ($amount == 0)
  { 
    echo "<b>Balance: &#8364;00.00</b>";
  }
  else
  {
    echo "<b>Balance: $amount</b>";
  }
}

答案 1 :(得分:1)

您要向$添加额外$amount,请尝试以下操作:

if ($amount == 0) {
   echo "<b>Balance: &#8364;00.00</b>";
} else {
  echo "<b>Balance: $amount</b>";
}

事实上,你可以使你的代码更具可读性/标准:

if ($amount == 0)
{
  echo "<b>Balance: &#8364;00.00</b>";
}
else
{
  echo "<b>Balance: $amount</b>";
}

答案 2 :(得分:0)

我在while循环中移动了if语句,清理了代码并删除了最后一行的额外$符号。

while ($row9 = mysql_fetch_array($result9)) {
    if ($row9['amount']) == 0 {
        echo "<b>Balance: &#8364;00.00</b>";
    } else {
        echo "<b>Balance: $row9['amount']</b>";
    }    
}