Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:121675 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 21532 invoked from network); 14 Nov 2023 19:06:41 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 14 Nov 2023 19:06:41 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 75815180087 for ; Tue, 14 Nov 2023 11:06:40 -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_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-f174.google.com (mail-yw1-f174.google.com [209.85.128.174]) (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 ; Tue, 14 Nov 2023 11:06:39 -0800 (PST) Received: by mail-yw1-f174.google.com with SMTP id 00721157ae682-5afbdbf3a19so68523337b3.2 for ; Tue, 14 Nov 2023 11:06:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dqxtech.net; s=google; t=1699988799; x=1700593599; 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=b+cBHCXkK/LDYEKapHU+6mg4zditSdN1/5gveMZJHUM=; b=gyTdBKYzGzTEjRWUYOsnUyQm6BTRQA8dhAUP0e/2tiMsRASPPLXNWTxuR4xI5PpTyA fIxkTlHkIsWpmySLiZDhswv0BZWdg7/Kl/CHzrmphGjKFnOt6+30jSbbQ2rHv4XaOAzI ZGreT3isVl4Nu/5YSeSD9nDYBxyIfew1tYnM097JbihFCR+Kkd4faVzaNKJjY1jV1C9r BTMPi6LXZY63JSNl+/ivVRn5JGyf4hLjAfXMG1qiBXa/SnO6SGGmCefWoYDH8wb48V8s aKfhS8xVcvdsTi0CGnY5QRiPoHaLsHLmhUPPBqgopWRAleFfeJLkw3vZJAHvNp818tUn OdoA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699988799; x=1700593599; 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=b+cBHCXkK/LDYEKapHU+6mg4zditSdN1/5gveMZJHUM=; b=MTEW4UH5LaSpue7KEaXFPVhoX7ElIXDRkeXgnGdjRJvfLsh6qX+7zDtIbeWad1ggE4 OfRSJZEhb2dnZkvuMuk26/yi8/Qh4F/AVexbE2IycGJ5tsxlndvcRPFUH/23y/9irpYl HjQRNmQfF6833xYM1Y9Kpg9qYjJ4w2u9W/EsLn78WZyu7q80o9T24zev7wXBrFFiuwW1 V6VepV68lG5CZ7t9sq7Fsy+Dr+3Xj4xlcujH23yI9pnJ327c4iqh4uF7PL5BvrxBe162 D5Ce8/zbGgfcVedLHGVW63VaOlZOYhWz9xO39AzDWNn7wQ/EOBJ0p3BbkW9m8UsVF3er eFKg== X-Gm-Message-State: AOJu0YwlY93jv1rVyBopIwGBDyA2MoUF0Ldr0+CEvGGOTn/NvKMZzJxM 43DdhMv8tB8S5a2l1+iGI9b/shjCYJdnH7AptkMwpjSLJjI2ljEbXhSt+w== X-Google-Smtp-Source: AGHT+IESOx9zk6DwF3K1mIRUldxinWXgfZfsiuuCXHrB6Cb2DxkcECwl3f8ROhRRAm8XeQ6kox1/ZbOr32G2pnXG8GA= X-Received: by 2002:a0d:ebc9:0:b0:5a8:dbb1:f73b with SMTP id u192-20020a0debc9000000b005a8dbb1f73bmr12068368ywe.14.1699988799110; Tue, 14 Nov 2023 11:06:39 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: Date: Tue, 14 Nov 2023 20:06:26 +0100 Message-ID: To: Robert Landers 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) On Tue, 14 Nov 2023 at 18:49, Robert Landers wro= te: > > On Tue, Nov 14, 2023 at 1:39=E2=80=AFPM Andreas Hennings wrote: > > > > Hello Robert, > > > > On Tue, 14 Nov 2023 at 11:09, Robert Landers = wrote: > > > > > > Andreas, > > > > > > Just out of curiosity, what is the use case for this? I can't really > > > think of a practical case where strict checking is needed for these > > > functions. Usually, you have a really good idea of what is in the > > > arrays when writing the code and can handle any edge cases (like > > > nulls, empty strings, etc) long before you reach for these functions. > > > > I could ask the reverse question: When do you ever need a non-strict co= mparison? > > I think in most modern php development, you would prefer the strict > > comparison version simply because it is more simple and predictable. > > > > But for real examples. > > One thing I remember is array_diff($arr, [null]) to remove NULL > > values, without removing empty strings. > > Perhaps we could say this is a special case that could be solved in > > other ways, because we only remove one value. > > > > Another thing is when writing reusable general-purpose functions that > > should work for all arrays. > > The caller might know the types of the array values, but the developer > > of the reusable function does not. > > > > Another problem is if your arrays contain anything that is not > > stringable. like objects and arrays. > > > > Maybe I will remember other examples that are more practical. > > > > Btw, as a general note on strict vs non-strict: > > In some cases you want a "half strict" comparison, where '5' equals 5, > > but true does NOT equal '1'. > > But for now I am happy to focus on pure strict comparison. > > > > Andreas > > > > > > > > > > Robert Landers > > > Software Engineer > > > Utrecht NL > > > > > > -- > > > PHP Internals - PHP Runtime Development Mailing List > > > To unsubscribe, visit: https://www.php.net/unsub.php > > > > > Hello Andreas, > > > Another problem is if your arrays contain anything that is not > > stringable. like objects and arrays. > > array_udiff() comes to mind here, is there a reason that doesn't work? > It would allow you to 'half-equals' things as well, if you want. Yes this would work, with a callback that does strict diff. But this is going to be much slower than a native array_diff() for large ar= rays. Also, array_udiff() is weird in that it it expects a sorting comparator function rather than a boolean comparator function. > > > I could ask the reverse question: When do you ever need a non-strict co= mparison? > > You actually answer your own question :) > > > where '5' equals 5, > > One of the most beautiful things about PHP is that null =3D=3D 0 =3D=3D f= alse, > or '5' =3D=3D 5 =3D=3D 5.0, or 1 =3D=3D true =3D=3D 'hello world', which = is so > incredibly handy in web-dev that to ignore it is inviting bugs. One problem of =3D=3D in php and in javascript is that it is not transitive= . And I would argue in most cases it does a lot more casting than we need, in a way that can be perceived as surprising. > Headers may or may not be set, form values may or may not be set, JSON > documents may or may not be missing keys/values, etc. How everything > is coerced is extremely well documented and very obvious after working > with PHP for a while. I imagine a user study would come to a different conclusion than "very obvi= ous". But ok. Anyway, time for an RFC. (I was going to write more, but I should protect this list from my preaching and arguing) Andreas > > I'm reminded of this principle in PHP, quite often: > > > Be conservative in what you do, be liberal in what you accept from othe= rs. > > Robert Landers > Software Engineer > Utrecht NL