Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:121347 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 3632 invoked from network); 17 Oct 2023 09:41:43 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 17 Oct 2023 09:41:43 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 5E583180044 for ; Tue, 17 Oct 2023 02:41:42 -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,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-f47.google.com (mail-ot1-f47.google.com [209.85.210.47]) (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:41:42 -0700 (PDT) Received: by mail-ot1-f47.google.com with SMTP id 46e09a7af769-6c61d955161so3282417a34.1 for ; Tue, 17 Oct 2023 02:41:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1697535701; x=1698140501; 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=UsRK3VJbVVNj/hLjVaCAncF6e8a2cFj/w6Adn4WXDBI=; b=ZSMn3DThgoDZSLLI3HsPvZt6DO3815xz/P9h0qUj71fq/eT9WXP2Mr1lpKuWxRCBgB XLZfvFQslitkuRSd7Ms1WQbrHcdcs8eLk2b2VNTCNX7WrQ09M4qCyAgMhqaAmzfVONvB QIIjicOSXFTCHzuHOiCNBkrBHavM5vDaWw/pq2LzNAXbus1/fLkYSDZ9H1fGO1/FleD7 ONKFlzXp10k2ycCQXtcOcr9ay0sZlKPuu/UFuzQHauxwD0I3Ilk4uE28b52dOT2RhYh+ H3bhv0daquZXoiK36vi2ZyEOXviqCo+weUGM+iCpK/bpXHmXwgwNfHrLdaxWNW733fUU NFYQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697535701; x=1698140501; 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=UsRK3VJbVVNj/hLjVaCAncF6e8a2cFj/w6Adn4WXDBI=; b=jFO8YmU+xLUgUj0ZR3b1nKF7SDJ7OG4JBm6fKXzO4lSUe28BOSqyKHHbBPAqwUlLm0 +3qdrJHcptkNnngvToKaYZV4m75VjO7VLZcCRK8iDoan+jR7Lkzf+nPu29nQg+SQ4MUE 1eYKcsfZfbYJvazOQQiPS6Ptl/8ujsjV1ZDLrb+Xf04Xm02aFPDszkCfo4vIWvyJeojM XAkoImpsyyg2/pMEqCmOV5hWDSIdd2tDIk3xQ+JUd8EkdQ2YYHQuz2F8MyexqMuM76Rg +R4g1GHnKOpEm4piP96ZoJBVzH2z2uYvsOUR12XKITAoQ/zwQ52Kgwe8BPh1ugkqkef/ FVjA== X-Gm-Message-State: AOJu0YwMymWU3Hi32YqGVlvVI/bCbz2pdNkAGei2EVFIJ7sbo9o/xPU9 FgSPa/P5hgO7oD+N9VF1dBLy+N7tbGaVajr1rQ8= X-Google-Smtp-Source: AGHT+IGhXA0knPy2P5yWkdwaNg2gAdiDO7nO89uwhRdGWaAZYee8NAajjcb4/HOe/qbu+V5r8yiK8wTQkWl28XzK+/g= X-Received: by 2002:a9d:4818:0:b0:6bd:a47:7bb6 with SMTP id c24-20020a9d4818000000b006bd0a477bb6mr1749196otf.14.1697535701483; Tue, 17 Oct 2023 02:41:41 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Tue, 17 Oct 2023 11:41:30 +0200 Message-ID: To: Aleksander Machniak Cc: internals@lists.php.net Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] Two new functions array_first() and array_last() From: landers.robert@gmail.com (Robert Landers) On Tue, Oct 17, 2023 at 11:37=E2=80=AFAM Robert Landers wrote: > > On Tue, Oct 17, 2023 at 11:34=E2=80=AFAM Aleksander Machniak wrote: > > > > On 17.10.2023 11:29, Robert Landers wrote: > > > $value =3D array_value_first($array, $key); > > > if($key =3D=3D=3D null) // handle error > > > else // do something with $value > > > > > > You can also freely ignore the key, if you don't need it or care abou= t > > > error checking. That would save doing two function calls on the same > > > array, just to do some error checking. > > > > > > > Please, no. What's wrong with count() or empty()? > > > > +1 for array_first() and array_last(). The only problem is probably a > > big BC break. I myself have array_first() defined in my framework. > > > > -- > > Aleksander Machniak > > Kolab Groupware Developer [https://kolab.org] > > Roundcube Webmail Developer [https://roundcube.net] > > ---------------------------------------------------- > > PGP: 19359DC1 # Blog: https://kolabian.wordpress.com > > > > -- > > PHP Internals - PHP Runtime Development Mailing List > > To unsubscribe, visit: https://www.php.net/unsub.php > > > > Hey Aleksander, > > > Please, no. What's wrong with count() or empty()? > > Nothing. Why not either one? You don't have to use the $key variable > and you can use count() or empty(), but for me personally, it makes a > lot of sense. Ah, I just realized why it makes a lot of sense, and that is when Fibers get involved. The value and key could change underneath you from one function call to another. $key =3D array_key_first($this-array); // call something that suspends a fiber and results in $this->array being mutated $value =3D array_value_first($this->array); // $value and $key may now point to two totally separate things. I've been bitten by this with Fibers a few times now (or things very similar to it). Having a way to atomically get $key and $value would be a boon, not a hindr= ance. Robert Landers Software Engineer Utrecht NL