Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:127075 X-Original-To: internals@lists.php.net Delivered-To: internals@lists.php.net Received: from php-smtp4.php.net (php-smtp4.php.net [45.112.84.5]) by qa.php.net (Postfix) with ESMTPS id B8E3D1ADCFB for ; Tue, 8 Apr 2025 18:25:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1744136590; bh=ZRSVkgtfiXTXl7RYO1g1UVy6jPMqCqpiPxlVO1Rcato=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=J26q6GfriffsfbiPtadz1pnMaTyyDJoeU0+o9XnvIUHx6iBpr8LxB6N9NKvmB829j yeF50SqA8MVp5FKU9nt+bzmNeypQ9LmSGi06A4xhgry1n9rYjGHRgFPD+m5jQJF0bu KX4CvOVWviwiyXlJLPkFVFSqU/HvcNKtvxUPYMCN++kkdvHiH4NbuJoJybVg0RrY89 tmM0UcfctDh78OuqZiEMVnjMOdrlLbYvbxpRK4eMc7bOxas/BbDVJjl0Pd9/mB6u41 0h6xZEqqMEf23Dm9ZGqCq4jtXnI6WN9PJSOTMQWuGQsc88V8cjveEUgz6bxCjhMcDE yl6h3nAiS0k7Q== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id CDF6C1801E5 for ; Tue, 8 Apr 2025 18:23:07 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.0 (2022-12-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-0.2 required=5.0 tests=BAYES_40,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from mail-yw1-f172.google.com (mail-yw1-f172.google.com [209.85.128.172]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Tue, 8 Apr 2025 18:23:05 +0000 (UTC) Received: by mail-yw1-f172.google.com with SMTP id 00721157ae682-6fead015247so53558767b3.2 for ; Tue, 08 Apr 2025 11:25:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dqxtech.net; s=google; t=1744136729; x=1744741529; 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=euwJ9UeLKz74qPIBQAqLTKmco/qSBRjAAKCQ5c//lmI=; b=NYINbQpRtLDFV6YbKO1FQ2Gv398C1D6olG/h8WBg+/SyIL0t04p6mr0oyETwxE8tW/ t1WnpYqaYkrH0q8deNe7JJjURD0b7+tIB2SCv8G3TlIPAkJ02uL3JzAY/jiIN1JKnqdZ 5vbfZ+ds67tEcFkqVFyKpkGnwM/IpYxJ2uEhGswpPG0tz5a/tskKSH+dL/qJ6jdU3RBz r9jWW1R+X8ORmLl6moPCiJH/qDTfucgJEV2wIsJZSs2ekd/RPr+Vb5PRaxOH8dWdqbeH 9TfZ99qj+DsR3yOdxVRkiYV5nx3xj9ZiY1sEE2UsVHht1zkewzex5tyHE5EWmgb2YowE /O1A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744136729; x=1744741529; 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=euwJ9UeLKz74qPIBQAqLTKmco/qSBRjAAKCQ5c//lmI=; b=bYQ3Op5TqarrUVOQwMeqV9Tgcg010x5I5Qyzx2/FVzgGDJr/kQ8lkshXZnPgTjN2mF QyH8z5jgjUuqzoDdfE+oUBdaQZGruVI/X0Y0Akphg6RX01+ZKR359qxcteTjg+dOUDnT i3mot2yL32Kn6nb339vuMPmDcyU14ApSFyWR876u1uZBluCvRUjGOIdtTyMH8tJSfeCx kUrtV2uM/e2WDikDk/0pz7yJvi/6ALlPX9ahNk7ON/tfxjpWNR6AeUX2q36rpbgzn13O 8IDcZTJRXIz1Xs0xuBKmkF8+akJVhLPo34wOh+fBn89b6MhNOX9Fq1NtqE2hEA6vEUch uj2g== X-Forwarded-Encrypted: i=1; AJvYcCX5kdYYO1ZMnv+7YghEwufvUVGuqw+2iL6Kk9IRVW1abBKE40uA/ONOn9K2tNWS+iab9v9ar/S87rg=@lists.php.net X-Gm-Message-State: AOJu0YyrNwN2GjNYF9Cj8yBWV0grlUbkjNVfd5PyUesvIGL9wnOxun80 XHRrQT/Ktw/LpM0+x3ilaf9RtymkvW1UR64puc9zy/+GqfcKCjk8ldCHLusEQf4YU05SHLZP+00 0IzSiaWNLIMmCkw6CJ5rEm3yw1tgOEXSD2Zcz8A== X-Gm-Gg: ASbGncvBx2JTGfN0aEB/xXLqOFZGaL9Gi2Ma2YbFqT1LipVeNM+LBWJIw4DX+RUFmq2 3oA2vIG2oSgdE8qMk9sKO9t8k7o/9DmJPZD7R1yYrc2kGKHTYW7tHQWw9X8zSsIR4oJvNWns08s 51E2rNzTk1CwT/KyN1yuFtXg+n X-Google-Smtp-Source: AGHT+IFjMfcJ+1y4Fwvpo1D180+LP5Yk17RIeXkprXhf+YcZdCNukzAC3m5+zkHPJCYkTPrKboCbAeVojH4h0mLmihI= X-Received: by 2002:a05:690c:6402:b0:6fe:b109:6965 with SMTP id 00721157ae682-705388f3dedmr4036397b3.35.1744136729685; Tue, 08 Apr 2025 11:25:29 -0700 (PDT) Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net x-ms-reactions: disallow MIME-Version: 1.0 References: <51fcec92-b2ae-482e-920f-c6c991b526c5@gmail.com> In-Reply-To: Date: Tue, 8 Apr 2025 20:25:18 +0200 X-Gm-Features: ATxdqUG-bpSgvrLIH3cDbhTPHjlG4H2lMsye5h4zac4sfGThaITmbYoVXlC-q2g Message-ID: Subject: Re: [PHP-DEV] [RFC] [Discussion] array_first() and array_last() To: Levi Morrison Cc: Niels Dossche , PHP internals Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable From: andreas@dqxtech.net (Andreas Hennings) On Tue, 8 Apr 2025 at 18:38, Levi Morrison wr= ote: > > On Sat, Apr 5, 2025 at 9:51=E2=80=AFAM Niels Dossche wrote: > > > > Hi internals > > > > I'm opening the discussion for the RFC "array_first() and array_last()"= . > > https://wiki.php.net/rfc/array_first_last > > > > Kind regards > > Niels > > I dislike all the functions where `null` is a valid value, which can > also be confused with an error in the operation. > > However, this is kind of in the grey area for me, because we do have > plenty of tools to avoid the error condition. Of course there's the > simple if: > > if (!empty($array) { > $first =3D array_first($array); > } > > And if we have a default value that could be used: > > $first =3D !empty($array) ? array_first($array) : $default; > > I would prefer a better design tool than returning null, but being > honest, neither of these are too bad. As long as our documentation for > these functions are helpful but concise about showing how to work > around these edges, then I may be convinced to vote yes despite the > design. (I used the wrong reply button earlier) The problem with the above solutions is that you need to repeat the array expression. For a simple variable that is ok, but for a call, not so much. So it is not good for nested expressions, you need to introduce a local variable. (which some believe you should do anyway, but...) One way to get a custom unique "empty" value would be this: $zilch =3D new \stdClass(); $first =3D array_first(some_lengthy_expression() ?: [$zilch]); if ($first =3D=3D=3D $zilch) { ... } We will know for sure that $zilch cannot be a pre-existing array value. In a lot of cases we already know that the array does not contain NULL, then it works fine as a default. -- Andreas