6

I was working here with some exploded string walks:

array_walk($data, function(&$value)
{
    // Transform numerics into appropriate type numbers
    if (is_numeric($value))
    {
        $value = substr_count($value, '.') == 1 ? (float) $value : (int) $value;
    }

    // Transform dashes into nulls
    if ($value == '-')
    {
        $value = null;
    }
});

to transform values into their appropriate types, and some special character handling.

Where I stumbled upon an interesting, huh, bug?

The Bug

I was amazed, that each entry, that had it's initial value as string(1) '0' ended up being a null.

At first, I thought that the problem relies in (float) and (int) typecasts, though, after debugging:

var_dump((float) '0', (int) '0');

I saw that's not the case, getting the expected result:

float(0)
int(0)

It took me a while, to attempt to debug the, what at the moment appeared to be an obvious, weak type check, but, once I did, I was shocked:

var_dump('-' == 0);

The above expression appears to be:

bool(true)

Now, while writing, I thought I should debug some more, so:

var_dump( '=' == 0 );
var_dump( 'What a lovely nonsense?' == 0 );
var_dump( 0 == 'DAFUQ?' ); // maybe it's because of order? It's PHP, world of miracles, you know...

And, every expression listed above is bool(true).

Okay, maybe that's because internally, mystically PHP casts the expression into a (bool)?

var_dump( (bool) '-' == 0 );

No:

bool(false)

So, so, so...

I made a test-case here: http://codepad.org/smiEvsDj
The problem exists in 5.2.5 (codepad), also in 5.4.3 (friend) and also in 5.4.17 (my actual environment).

What is the reason behind this feature / bug / what-the-F-actually-is-this?

tomsseisums
  • 13,168
  • 19
  • 83
  • 145
  • possible duplicate of [Why does PHP consider 0 to be equal to a string?](http://stackoverflow.com/questions/6843030/why-does-php-consider-0-to-be-equal-to-a-string) – devnull Aug 16 '13 at 09:09
  • Just read the [manual pages](http://www.php.net/manual/en/language.operators.comparison.php) about loose typing and get over it.... this supposed `issue` with loose-type comparisons has been hashed to death over the years – Mark Baker Aug 16 '13 at 09:10
  • `If you compare a number with a string or the comparison involves numerical strings, then each string is converted to a number and the comparison performed numerically.` [Source](http://php.net/manual/en/language.operators.comparison.php) – devnull Aug 16 '13 at 09:11
  • I have only one thing to say... *DUCK* PHP! – tomsseisums Aug 16 '13 at 09:20

2 Answers2

2

You have stumbled upon one of the major complaints that people have about PHP as a language: The fact that the "==" operator is not transitive.

Any string "foo" == TRUE, because the PHP people wanted this to work:

if ($string) {
    // do something if $string is set
}

Yet, converting a string to a number (which PHP always tries to do when you use "=="), "foo" == 0!

Of course, TRUE != 0. That is a major pain when dealing with PHP, it's not logical, but it's reality.

ciruvan
  • 5,143
  • 1
  • 26
  • 32
0

It's trying to parse numbers from your strings, not finding any digits and automatically reverting to 0. I think...

e.g. it sees 'What a lovely nonsense?' == 0, sees that you're comparing integers, and tries to convert What a lovely nonsense? to an integer. As there are no numerical digits it defaults to 0, and believes that LHS == RHS so returns true

Mattsjo
  • 627
  • 5
  • 11