Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:121659 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 13626 invoked from network); 11 Nov 2023 19:43:13 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 11 Nov 2023 19:43:13 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id CF3851804C1 for ; Sat, 11 Nov 2023 11:43:12 -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,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE 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-yw1-f170.google.com (mail-yw1-f170.google.com [209.85.128.170]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Sat, 11 Nov 2023 11:43:12 -0800 (PST) Received: by mail-yw1-f170.google.com with SMTP id 00721157ae682-5a86b6391e9so38070857b3.0 for ; Sat, 11 Nov 2023 11:43:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dqxtech.net; s=google; t=1699731791; x=1700336591; darn=lists.php.net; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=XWrk+qJV/CxIG4DaAbYAwHzBMyqaaaqMIn3zw/TMT/A=; b=aFtv3NZ/NcdlFtO+jkXYL0+XhfvjJ+AOQQFubAi3xsYrGaFh0crSlBkLVcGmk3INdG AgPsgYQd3r7nwZI7zzLKn7QhU9Y87kdtw03WRFFS60CUmdZzjRv8a2OHTXH0U3y5wvmR xpyZdKiVc2pge2eTP5hDfLuoka7b+ghZC0x05aO2Qd03EmxsXX78w4XazNqSruZqAdfu 0yOyY2hm8AI3xOZZSMDvMz4FagSCkRgwgvyCPtbwLElhRgOQQDSqMyCvhgMkDyd1lvoc 3y7vz9s2d9PczV2nlnL/jo38NumgnCZA3B1LGtsr/HOOvrSp2MJfXgHPt/UUoLcaH+kq dBBw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699731791; x=1700336591; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=XWrk+qJV/CxIG4DaAbYAwHzBMyqaaaqMIn3zw/TMT/A=; b=A1DnHU5joAxMgSg/WDrdwBV3EBgIyB2n6/YNhhGHViQ14cmlfz/Gu8r2bVUuyJYHDk RRwv4y4DYMZVLjnopLOjID/ljisrKSkrpp+q1APKga3X2E7XPqo5/Er86hgl0IUGsvOU +35KK3/KLZxKiknKJR2iXSFbYof71FyQ8YHBk69eJylG7dzfdQhCwr9FXB2bMYZeBpfE tQKc1h+5C150X1DVXRDxwtbVtlH9bmXTBNZOkVXHlUWBtlDnUBh5atIWduOXOxrXhwmh fuwdYmdDf8NNacn14sSLkCBIBss3WTMf3PiYIe0ilf4mU5Q9A6GT04db+LmjuQsAwD6z UmBg== X-Gm-Message-State: AOJu0YyHPkzcIYpDeC/z4Z247bWyIT/BjPnktVS8ijd6cc9Rf+Yij0yN B/oX5aMwnBnRp2R/dlOpOdVwQv/lNjj5pKqIrClt1w== X-Google-Smtp-Source: AGHT+IGY/QDzvO4ldh7fkHkoH13hWeSoUfQ4+J9p3igewndPZjmxf/ULMxuW82/iZFfbsi7alBjmufbLddXsFTd+P2g= X-Received: by 2002:a0d:dc42:0:b0:59e:8f6d:92e with SMTP id f63-20020a0ddc42000000b0059e8f6d092emr2836298ywe.49.1699731791395; Sat, 11 Nov 2023 11:43:11 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: Date: Sat, 11 Nov 2023 20:43:00 +0100 Message-ID: To: David Gebler Cc: PHP internals Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] Array functions with strict comparison From: andreas@dqxtech.net (Andreas Hennings) Hello David, On Sat, 11 Nov 2023 at 20:04, David Gebler wrote: > > On Sat, Nov 11, 2023 at 6:05=E2=80=AFPM Andreas Hennings > wrote: > > > Hello internals, > > I noticed that array functions like array_diff(), array_intersect() > > etc use weak comparison. > > > > > That's not quite correct. Using the example of array_diff, the comparison > is a strict equality check on a string cast of the values. So > array_diff([""], [false]) will indeed be empty > but array_diff(["0"],[false]) will return ["0"]. Thanks, good to know! So in other words, it is still some kind of weak comparison, but with different casting rules than '=3D=3D'. Still this is not desirable in many cases. > > Tbh any use case for whatever array function but with strict comparison i= s > such an easy thing to implement in userland[1] I'm not bothered about > supporting it in core. But that's just me. I don't generally like the ide= a > of adding new array_* or str_* functions to the global namespace without > very good cause. There is a precedent for it though, in terms of changes > which have gone through in PHP 8, such as array_is_list or str_starts_wit= h. I would argue that the strict variants of these functions would be about as useful as the non-strict ones. Or in my opinion, they would become preferable over the old functions for most use cases. In other words, we could say the old/existing functions should not have been added to the language. (of course this does not mean we can or should remove them now) Regarding performance, I measure something like factor 2 for a diff of range(0, 500) minus [5], comparing array_diff() vs array_diff_strict() as proposed here. So for large arrays or repeated calls it does make a difference. Regarding the cost of more native functions: Is the concern more about polluting the global namespace, or about adding more functions that need to be maintained? I can see both arguments, but I don't have a clear opinion how these costs should be weighed. Cheers Andreas > > [1] Example: > > function array_diff_strict(array $array1, array ...$arrays): array > { > $diff =3D []; > foreach ($array1 as $value) { > $found =3D false; > foreach ($arrays as $array) { > if (in_array($value, $array, true)) { > $found =3D true; > break; > } > } > if (!$found) { > $diff[] =3D $value; > } > } > return $diff; > }