php-src icon indicating copy to clipboard operation
php-src copied to clipboard

string '0.0' evaluates to true

Open ghost opened this issue 4 years ago • 1 comments

Description

Surely this is not a mistake, but I do not understand why it is so.

Consider the following code:

   if (0) echo "int 0 is true" . "\n";
   if ('0') echo "string '0' is true" . "\n";
   if (0.0) echo "float 0.0 is true" . "\n";
   if ('0.0') echo "string '0.0' is true" . "\n";

Why does the string '0.0' evaluate to true, when its logical value would be false? The string '0' evaluates to false. Why is the same not true for '0.0'?

https://3v4l.org/ZNhbM

PHP Version

PHP 7.X, 8.X

Operating System

All

ghost avatar Dec 10 '21 15:12 ghost

Well, that's just long standing behavior; back then it was decided to treat "0" and "" as false, but no other strings.

Anyhow, such questions are better asked on StackOverflow etc. :)

cmb69 avatar Dec 10 '21 16:12 cmb69