Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:121313 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 5741 invoked from network); 14 Oct 2023 18:01:00 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 14 Oct 2023 18:01:00 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id BB53A180041 for ; Sat, 14 Oct 2023 11:00:57 -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=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL, SPF_HELO_NONE,SPF_NONE,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-ej1-f42.google.com (mail-ej1-f42.google.com [209.85.218.42]) (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, 14 Oct 2023 11:00:57 -0700 (PDT) Received: by mail-ej1-f42.google.com with SMTP id a640c23a62f3a-9b27f99a356so105114166b.0 for ; Sat, 14 Oct 2023 11:00:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=grudl-com.20230601.gappssmtp.com; s=20230601; t=1697306455; x=1697911255; darn=lists.php.net; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=7NJG8MmEQ3lZTgAwLabpi5FH/T0GqqIZywWKG9gpw9w=; b=hENJ1ixGrqb1JJn1AsaXDqq5fjPBn2guJ6Eaher+fY/gUz1TmXKEDN9esItoNMYWRW AnbxiQnbxgjnIFRqjXoNOCKHj5jK47Gm50oACAbSqZG2QU51G7ocdw6BaonUi2but6FB HHtOalI1keceUgKFNXPYDfwFg5EbOz/UpojHR6ebO4rGEDlycGcoxyX2N0E0uKQdERMd uTss8NPWDkr46Fds+Iw7kVtnsT1AkwZg7DNLtezTLTAbHLDM1T2EV6oPAlBWEldzfH6F QAtfMVvmQBl+SFJI4So0bGZlLovGEoKdWNnK+JJ9H2rU4KXa9hZq9ur0JXvr9EIuefVF kRTA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697306455; x=1697911255; h=to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=7NJG8MmEQ3lZTgAwLabpi5FH/T0GqqIZywWKG9gpw9w=; b=a4Ev9rBIihInUKRZuWfT6/94yRrc4mludNP6+O/yCe8lx48Z7h4IKMhPm9s76IO35p 2ey23FnZ1t/FuRelXL8heeQvJn3WSy+fZ3mcktZxZN7neB9iQekKpNWBGT+Eats8pGJH rsUoY0PEq60HT5arghQ7/V1jyzVPB22nbiKx3dbU7nG2ttQMRQqWxj9b5ILChPGme9GH FCRrM4e4vfEmSNXgPJOJD2y62pm8JfTtzsNZ+B6oyl0fHSWo0/mrf3spJEvAU9nCbyF3 ilQzJ1EpIdEutLfpL7/+afbWo22DegLJeB+IY1nPfjfchbu77PSTTs0sbyYUkqmK78ed aOsQ== X-Gm-Message-State: AOJu0YzHA4mrB9Xmz/sCy/jQCcSnzAxaPWkYRdzUnAbnvNVZhVip8zoO COWVRGLfn3uZC1B4PhU/pJ5u7U8NaiJ3SE5hZeb9anNmYNklKMnIMQU= X-Google-Smtp-Source: AGHT+IHam505VYmyoomHNqFiQEZD5v9GIlUZKCsn40qI7pNEzdNKUpYxMHORi1TN8wNXdX1H9sQnkbA+pgKFab5UOSE= X-Received: by 2002:a17:906:2254:b0:9ae:50de:1aaf with SMTP id 20-20020a170906225400b009ae50de1aafmr27308731ejr.4.1697306455117; Sat, 14 Oct 2023 11:00:55 -0700 (PDT) MIME-Version: 1.0 Date: Sat, 14 Oct 2023 20:00:42 +0200 Message-ID: To: internals@lists.php.net Content-Type: text/plain; charset="UTF-8" Subject: Two new functions array_first() and array_last() From: david@grudl.com (David Grudl) PHP lacks two very basic functions for working with arrays: - array_first() returning the first element of an array (or null) - array_last() returning the last element of the array (or null) While PHP has functions that return the first and last keys, array_key_first() and array_key_last(), it does not have more useful functions for values. a) What about reset() and end()? Programmers "abuse" the reset() and end() functions for this purpose. The problem is that these functions are used to move the internal pointer in the array. Which is why they have a name that is inappropriate when used in the sense of "return me the first element". Much worse, they shouldn't to be used to get first/last value, because they have a side effect (i.e. moving the pointer). Further, in the absence of an element, they return the obsolete false and not the currently expected null, which can be combined with the ?? operator. In this they differ from the similar functions array_key_first() and array_key_last(). b) What about $array[array_key_first($array)]? For such basic functions as returning the first and last item in an array, there should be a function in the basic package, not a workaround. Moreover, this requires having the array in a local variable, since $this->getFoo()[array_key_first($this->getFoo())] would be very inefficient and possibly incorrect. 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. DG