PHP echo text if variable is blank
I am trying to echo €00.00 if my variable $amount equals zero
I have done something wrong can you help??
while ($row9 = mysql_fetch_array($result9))
{
$amount = $row9['amount'];
}
//$amount = $amount / 60;
//$amount = round($amount, 2);
if $amount == 0 echo "<b>Balance: €00.00</b>";
else
echo "<b>Balance: $$amount</b>";
You need to put the if/else in the loop, and you have some invalid syntax (missing parens and double $). So:
while ($row9 = mysql_fetch_array($result9))
{
$amount = $row9['amount'];
if ($amount == 0)
{
echo "<b>Balance: €00.00</b>";
}
else
{
echo "<b>Balance: $amount</b>";
}
}
You are adding extra $
to the $amount
, try this:
if ($amount == 0) {
echo "<b>Balance: €00.00</b>";
} else {
echo "<b>Balance: $amount</b>";
}
In fact you can make your code a bit more readable/standard like this:
if ($amount == 0)
{
echo "<b>Balance: €00.00</b>";
}
else
{
echo "<b>Balance: $amount</b>";
}
I've moved the if statement inside the while loop, cleaned up the code and removed the extra $ sign that was on the last line.
while ($row9 = mysql_fetch_array($result9)) {
if ($row9['amount']) == 0 {
echo "<b>Balance: €00.00</b>";
} else {
echo "<b>Balance: $row9['amount']</b>";
}
}
精彩评论