Home > Software engineering >  Why does 0.00 evaluates to true?
Why does 0.00 evaluates to true?

Time:04-26

I wanted to write a very simple function display some values only if they evaluate to true. So I wrote the piece of code below, and some test function calls...

function value_fff($value = false)
{
    return $value ? $value : '';
}

echo value_fff('fsdfsdf') . '<br />';
echo value_fff('0.00') . '<br />';
echo value_fff('0') . '<br />';
echo value_fff(false) . '<br />';
echo value_fff(null) . '<br />';
echo value_fff('1') . '<br />';
echo value_fff() . '<br />';
echo value_fff(true) . '<br />';

My problem is that I get the following values:

fsdfsdf
0.00



1

1

So, it's obvious that 0.00 evaluates to true... Why is that happening? Indeed I'm in a locale that decimal separator is , instead of ., but is it that? And if yes, how am I supposed to deal with it?

CodePudding user response:

function value_fff($value = false)
{
    if (is_numeric($value)) { $value  = 0; }
    return $value ? $value : '';
}

tried adding is_numeric() to check if the string is number then we're gonna add it to 0 just to make it a number.

  •  Tags:  
  • php
  • Related