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

in_array string checking error......

Open chiucs123 opened this issue 4 years ago • 3 comments

Description

The following code:

<?php

var_dump(
    in_array(
        'any content',
        ['', 'null', 'false', 0, '[]', '{}']
    )
);

Resulted in this output:

bool(true)

But I expected this output instead:

bool(false)

PHP Version

7.4.24

Operating System

Windows 11

chiucs123 avatar Jan 27 '22 04:01 chiucs123

Use the third parameter.

https://www.php.net/manual/en/function.in-array.php

damianwadley avatar Jan 27 '22 04:01 damianwadley

I understand using the 3rd parameter would bypass the issue, but this is an obvious bug to be fixed. The needle content has none possible ways to type adjust match any item in the array.

chiucs123 avatar Jan 27 '22 04:01 chiucs123

The function is behaving exactly as intended, as designed, and as documented: by default it performs loose comparisons against array items, and thus it would return true for your inputs because "any content" is loosely equal to 0. If you want strict comparisons instead of loose comparisons then you must provide the third argument.

damianwadley avatar Jan 27 '22 04:01 damianwadley