Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:121673 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 15695 invoked from network); 14 Nov 2023 17:49:25 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 14 Nov 2023 17:49:25 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 71D22180087 for ; Tue, 14 Nov 2023 09:49:24 -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, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,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-ot1-f48.google.com (mail-ot1-f48.google.com [209.85.210.48]) (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 09:49:23 -0800 (PST) Received: by mail-ot1-f48.google.com with SMTP id 46e09a7af769-6cd09663b1cso3754487a34.3 for ; Tue, 14 Nov 2023 09:49:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1699984163; x=1700588963; 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=7I+4MKSLh0hExSQZyqRdninEFfys9Nscjt2qNGRHR/U=; b=QsYcODKWW2ZBS3FJaLJAZBu4M7vCWE0nCqKazQcSeBtb/TP11JgQRxA29l3N3qX548 Xv2OaSxsham8UsrjVUkhyaVcYM5uvvIrQeOAsnXQ4CjenxBJuQtxiYSW/MA82/zMo2Tb mhc9WVfewIz59LmnhDG20LePPZJXJm1TmbywQaRSU47RwWo0EZOVIHVQNkAEjndZ6moV XFv3ggTVwHSbSDlCOyj9HzVzYcvQDf8DsGT6nhn4D7+Cp9+cppC4gD6gOC7Tnh7H/m3n GT/JCrKGvc1BqaA8yn0SMZtvP1cipBZZMg6e0zjuu7Bqd5p516hHu07PNAYQxgWQzSGd t7eQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699984163; x=1700588963; 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=7I+4MKSLh0hExSQZyqRdninEFfys9Nscjt2qNGRHR/U=; b=b83xt6DuLtM70+XFdswspc5Ii4krpb+tgqutJKnr+XDK8skWloSvlKA4nOIfVa/jss 8ce/Oele8nxO1Tx7IvOq3+fpLc3YfAfnrx29yA+YOe/5QozogOS9PsJVzbEEHzZCgbkH 2A5X2HPW3szxNR/V7Ev4RUElQoebARRhkA3gHyuLjd4fnwGdvGuR4wHq636wfr4CKTxL ZziV51612NTC6fjSyZxZCjjuilGuCI7XWBHh6ztqL+mIQ31yUNZil9bM78EXPW3w6g+w 0s6e5bqP1Heef9dMzKxAcBJVLWR2g/I4B+WV2tX8g0R/5PThX8/X+ivri2g3FTgAaQH+ CYvg== X-Gm-Message-State: AOJu0Yxetz2Dz9HErkFX6GfXzr7xIzuYc3SrVZZCsB6eYpI/BMtRySs0 hbXSa7V0OGWid5831ZUaUr4KoUBeE47z97CYE5r97JU/vFliPg== X-Google-Smtp-Source: AGHT+IFOpGMKqVtk6XRCmxAJVFekyNcvX6DYMFNeAsf7sNoyMIVrBXsmbrcD846Ft+bIf8o++QLS47sHf1JTzbp0GiE= X-Received: by 2002:a05:6830:1349:b0:6c0:a95b:44e4 with SMTP id r9-20020a056830134900b006c0a95b44e4mr2776672otq.38.1699984163056; Tue, 14 Nov 2023 09:49:23 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: Date: Tue, 14 Nov 2023 18:49:12 +0100 Message-ID: To: Andreas Hennings 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: landers.robert@gmail.com (Robert Landers) 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 w= rote: > > > > 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 comp= arison? > 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. > I could ask the reverse question: When do you ever need a non-strict comp= arison? 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 fal= se, 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. 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'm reminded of this principle in PHP, quite often: > Be conservative in what you do, be liberal in what you accept from others= . Robert Landers Software Engineer Utrecht NL