php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #80701 NaN equals NaN in arrays, if two arrays have the same identity
Submitted: 2021-02-03 09:47 UTC Modified: 2021-02-12 16:04 UTC
From: riikka dot kalliomaki at gmail dot com Assigned:
Status: Open Package: Arrays related
PHP Version: 8.0.1 OS:
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
MUST BE VALID
Solve the problem:
11 + 41 = ?
Subscribe to this entry?

 
 [2021-02-03 09:47 UTC] riikka dot kalliomaki at gmail dot com
Description:
------------
TL;DR; "$a = $b = [NAN]; [NAN] !== [NAN] && $a === $b".

PHP arrays don't officially have an identity. However, for optimization PHP uses copy on write for arrays so two arrays can have the same "identity" when, for example, an array is assigned to another variable.

For optimization, when using the strict comparison operator "===", PHP checks if the identity of two arrays is same to quickly return true in those cases. I mean, one would expect that two arrays that point to the same thing would be strictly equal.

However, this assumption is not true for NaN, i.e. [NAN] !== [NAN]. But, if you first assign the [NAN] to a variable, it can equal itself, if two arrays have the same identity.

Based on https://3v4l.org/gHjG0, this seems have been introduced in 5.3.15 / 5.4.5

I would consider this behavior to be a bug, but I'm not sure whether it is worth fixing since that could have non negligible performance impact. I did not run into this bug in actual code and simply realized it through personal musing. Thus, at least I don't personally consider fixing it important.

Test script:
---------------
<?php

$NaN = sqrt(-1);

var_dump($NaN === $NaN);
var_dump(array($NaN) === array($NaN));

$a = array($NaN);
$b = $a;

var_dump($a === $b);

reset($b);

var_dump($a === $b);

Expected result:
----------------
bool(false)
bool(false)
bool(false)
bool(false)

Actual result:
--------------
bool(false)
bool(false)
bool(true)
bool(false)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-02-12 16:04 UTC] cmb@php.net
Interesting edge case!  The equality operator has the same issue.
 [2021-09-24 08:14 UTC] graefrath at femu dot rwth-aachen dot de
I was just about to report the same issue! Maybe the following test script is a bit more concise:

<?php
$array = [NAN];

var_dump($array === $array); // always true, should be false in my opinion
var_dump([NAN] === [NAN]);   // always false
?>

This behavior is contrary to what the documentation says. It states that two arrays are strictly equal if they "have the same key/value pairs in the same order and of the same types."

I also consider this a bug. At the very least, there should be a note about this behavior in the documentation (possibly on the Array Operators page).
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 18 23:01:27 2024 UTC