Php echo text if variable is empty

I'm trying to echo € 00.00 if my $ amount variable is zero

I did something wrong, can you help?

The 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
source share
3 answers

You need to put if / else in the loop, and you have incorrect syntax (missing parses 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>";
  }
}
+3
source

If you are adding extra $to $amount, try the following:

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

In fact, you can make your code more readable / standard as follows:

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

I moved the if statement inside the while loop, cleared 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>";
    }    
}
0
source

Source: https://habr.com/ru/post/1746788/


All Articles