Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:116736 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 43517 invoked from network); 29 Dec 2021 15:53:08 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 29 Dec 2021 15:53:08 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 71E171804C3 for ; Wed, 29 Dec 2021 08:58:49 -0800 (PST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,HTML_MESSAGE, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS15169 209.85.128.0/17 X-Spam-Virus: No X-Envelope-From: Received: from mail-il1-f180.google.com (mail-il1-f180.google.com [209.85.166.180]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Wed, 29 Dec 2021 08:58:49 -0800 (PST) Received: by mail-il1-f180.google.com with SMTP id d3so9634905ilr.10 for ; Wed, 29 Dec 2021 08:58:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=JD8Vn8cp6QbC5qqMrVj3cGX0/X3JGXgzriYPyeXnmJk=; b=ZSyY3ZPS+uGz6VFCaPpOQrHPA90Q3yrIOOhCq871pYzwbJW+k4R2dJTd1QtdnfSpsJ GAC0M4Zh8fRLvrqeTIZRmdiJuzCjWUrTUN0zcgTEmQ4EXj3o9ALHSUepiuReEgNycBK6 GZlcBl91Piv0wjMVZBLnwk5lpRMz05LGCZ3SfB8GXnG12cMV/eyJ8gW0PuqbwC/T6jRI 2gnTgSLeZbDlhsHTCkG2Idg5WjzzeVGrVu5t4LggfMpaj9KEuwaHTJCIoXuhDY/CtgoA e23Y2PF6bX6Ju+nn3enF9MsXC665ciSCuchsWVPuN9P4+0MRDCm6oVwHNGEnrf6XGgjN ZPdw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=JD8Vn8cp6QbC5qqMrVj3cGX0/X3JGXgzriYPyeXnmJk=; b=bBjbWXFsM1neaJINpd7GfsIl+MqbBs+7Cn28+fmEDyNy2g2g5x2qtWzgt4Lf6Y+fFJ BXXj/RwrwdpwOt7diuczcI1+BXYAJBDzmTxf1oy6X7us8PWywitNyEfpJC5+hUAIqS8B XW1Lf1vKfTfbN746Lvbic/6csGc3KVeNCQif6B4hfivqDwE552JTizBX6VyJrI4MRVv5 qAWi6c1UbO6dyCDN85MOR7+xcMFgoANPtofOTASaEsrYBuRTNG9BzSmgGCfPqPlS3qNv K1CyeNMws7Q82ufCYX2yE6ycJ5UX2gpR6Jjla06zCXCj5Jk9HiS6CWhu0YhxOnnVMEQE zL8Q== X-Gm-Message-State: AOAM532XN5LoDcl1feCY0MypUlrwwXEneUF6tsJYuAC2wDlYkzJXy8oh /HJCzjwjlMUvDU88Ue2qSflstxT+orXgoABSnbgn/1WI4lw= X-Google-Smtp-Source: ABdhPJzG7S6gv/iT5jXzZQRLuHKQSPn0hb2hwTVW1g3LmqJEYxS/2ki8NyNvxVQgiQ9rsBt8/sEsHxBxLz7MpKUP4Yo= X-Received: by 2002:a05:6e02:170c:: with SMTP id u12mr12262648ill.53.1640797128461; Wed, 29 Dec 2021 08:58:48 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: Date: Wed, 29 Dec 2021 17:58:21 +0100 Message-ID: To: Vincent Langlet Cc: PHP internals Content-Type: multipart/alternative; boundary="000000000000fd91a405d44bd983" Subject: Re: [PHP-DEV] RFC: Stop to automatically cast numeric-string to int when using them as array-key From: kjarli@gmail.com (Lynn) --000000000000fd91a405d44bd983 Content-Type: text/plain; charset="UTF-8" On Wed, Dec 29, 2021 at 4:42 PM Vincent Langlet wrote: > Hi, > > I recently discovered that an array was automatically casting > numeric-string keys to int if it was possible. For instance, the following > array: > > $a = ['01' => '01', '10' => '10']; > > Is not an array with the key '01' and '10' but instead consider the second > key as an int. > > This has some implications like the fact that > - array_flip(array_flip($a)) !== $a > - array_search('10', $a) is an int when array_search('01', $a) is still a > string. Someone using strict types and passing the result to a function > expecting a string could end with an unexpected crash. > > I've created an issue about this > https://github.com/php/php-src/issues/7845 > but it was recommended to me to send a mail to this mailing list instead. > > I don't think this behavior should be considered as "normal" and would like > to propose to change this for PHP 9, as it's a BC-break. To me it can and > be considered as a follow-up of > https://wiki.php.net/rfc/string_to_number_comparison and > ttps://wiki.php.net/rfc/saner-numeric-strings > . This is still a > "concept" > since I never code with C and know nothing about the PHP implementation and > if this change would be possible. Any help is welcome then. > > Thanks > Hi, While I'd love for this inconsistency to go away, I also know that this is most likely such a big change that it causes months of work and broken code because it relies on this behavior. It wouldn't surprise me if this singular change would cause more work than all previous BC breaks combined. This behavior is documented: PHP: Arrays - Manual The key can either be an int or a string. The value can be of any type. > > Additionally the following key casts will occur: > > - Strings containing valid decimal ints, unless the number is preceded > by a + sign, will be cast to the int type. E.g. the key "8" will actually > be stored under 8. On the other hand "08" will not be cast, as it isn't a > valid decimal integer. > - Floats are also cast to ints, which means that the fractional part > will be truncated. E.g. the key 8.7 will actually be stored under 8. > - Bools are cast to ints, too, i.e. the key true will actually be > stored under 1 and the key false under 0. > - Null will be cast to the empty string, i.e. the key null will > actually be stored under "". > - Arrays and objects can not be used as keys. Doing so will result in > a warning: Illegal offset type. > > --000000000000fd91a405d44bd983--