Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:121341 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 94287 invoked from network); 17 Oct 2023 09:10:32 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 17 Oct 2023 09:10:32 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id EE471180087 for ; Tue, 17 Oct 2023 02:10:30 -0700 (PDT) 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-il1-f182.google.com (mail-il1-f182.google.com [209.85.166.182]) (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, 17 Oct 2023 02:10:30 -0700 (PDT) Received: by mail-il1-f182.google.com with SMTP id e9e14a558f8ab-3575c10421aso20535845ab.3 for ; Tue, 17 Oct 2023 02:10:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=datadoghq.com; s=google; t=1697533829; x=1698138629; darn=lists.php.net; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=9lVAvkwdLavUYAvqoTe5VmzsXQRkL7ARxrdFzwY5gEU=; b=JH96H/1ciNOmf/whuCJPVrMRTMRJQfX068f/5eHZ6IJckeP8yucicb5kJcQU0NIkp5 1w+MhXhnVSAUiK8/EHp7OoJ+kRUcJqBbgkyT+dKiA6nxVQa/n51qbfmApRTjxamyXOCk iC7wVEoQ2S9OfYjPjMLWj3UhUMEGl6M2M8ml8= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697533829; x=1698138629; h=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=9lVAvkwdLavUYAvqoTe5VmzsXQRkL7ARxrdFzwY5gEU=; b=jup/KlAOAdfky4BH36IRtAEWftnfWHHHMoyVCD0kIAe+lJLFHm+qcJtiNJRVcUVRjg EyjihFFZotCF7Sxjh/YU1gEqeWOjDw+pCkQLvPUiHjLePK+f/+u/QF6TRCTPD3NT5PYy IFODmknSXOly6DzVNU0JU46EU1DjrVn0DgEHZVZL1fdDDaiqJCNRshlSPEnvaG1yS7KR FZ8znI39R85SN3Jyzrdk8pyYoU9DWAj+wKaPwQb4lqOP31r/oUdrmRba8XC0dTzbfC0l 3U8WSr6tEPQQ4GSATWmYcwiYmIFwWxUCrtQG0Ch/goqxNZkUCx5cVZuAjB9mfLj+EUiP ZWkw== X-Gm-Message-State: AOJu0YxT2V9kNhsFa+j86z5JntbX8TiWPLZ9OjxvnSY3VqvkN3VNS2V/ zEwhLDh6Y9K9uWgpxPIZXyGhzYnTgfXuk3qvOKdYcSgkPnGRnowaArpDBbPx X-Google-Smtp-Source: AGHT+IEeLkiR+/ozatYDaFgrTq/tX+EL/gvQMJ3FS5+W5j2BvrO45ZcyzFDiEnLId1CMRQ7k8leLJCopd09fyMAk4l8= X-Received: by 2002:a05:6e02:805:b0:357:429e:e83f with SMTP id u5-20020a056e02080500b00357429ee83fmr1773421ilm.29.1697533829356; Tue, 17 Oct 2023 02:10:29 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Reply-To: Levi Morrison Date: Tue, 17 Oct 2023 11:10:18 +0200 Message-ID: To: David Grudl Cc: internals@lists.php.net Content-Type: text/plain; charset="UTF-8" Subject: Re: [PHP-DEV] Two new functions array_first() and array_last() From: internals@lists.php.net ("Levi Morrison via internals") > c) Two such functions were proposed and rejected during the > array_key_first/last RFC > (https://wiki.php.net/rfc/array_key_first_last) > > Yes, that was in 2018. At that time, functions like str_contains() or > str_starts_with() wouldn't have even come into existence, just because > there was an obscure way to do it without them. I believe we've moved > on since then. Today we know how useful it is to use simple, > easy-to-understand methods, both for programmers who write and read > the code. It's true that sentiment may have shifted in this time. However, a common argument at that time still stands: `null` is not a good sentintenal for failure because the value inside the array very well could have been null. This is not true for the keys. For me personally, I think I would still vote no. I'm not entirely sure about that, but that's how I would lean right now. As it stands, you'd have to write code along the lines of: ```php $key = \array_key_first($array); if ($key === null) { // handle the failure } else { // success $value = $array[$key]; } ``` Yes, it would be slightly nicer if we could do: ```php $value = \array_first($array); if ($value === null) { // handle the failure } else { // success } ``` But I fear in practice people will just omit the error checking. One way around that is to throw an exception. I'm not sure how I feel about that, but I'll think about it.