Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why does 0.00 evaluates to true?

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading