3

I am trying to echo €00.00 if my variable $amount equals zero

I have done something wrong can you help??

Code

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 Answers 3

3

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: &#8364;00.00</b>";
  }
  else
  {
    echo "<b>Balance: $amount</b>";
  }
}
Sign up to request clarification or add additional context in comments.

Comments

1

You are adding extra $ to the $amount, try this:

if ($amount == 0) {
   echo "<b>Balance: &#8364;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: &#8364;00.00</b>";
}
else
{
  echo "<b>Balance: $amount</b>";
}

5 Comments

The parentheses around the condition are required. It's not a question of readability.
The parentheses are still missing the in the first code example.
@waiwai933: I had corrected an error in the OP's code, posted his code there and that is the reason later I suggested him a better coding style.
@Web No doubt. Nevertheless, you still were suggesting that he use the first example, which was the reason we were pointing that there were no parentheses.
In your last example, if the database returns '0.00' for amount, if (!$amount) will evaluate as false.
0

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: &#8364;00.00</b>";
    } else {
        echo "<b>Balance: $row9['amount']</b>";
    }    
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.