Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:121351 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 25207 invoked from network); 17 Oct 2023 15:53:57 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 17 Oct 2023 15:53:57 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 4CB141804B0 for ; Tue, 17 Oct 2023 08:53:56 -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,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS34788 85.13.163.0/24 X-Spam-Virus: No X-Envelope-From: Received: from dd44434.kasserver.com (dd44434.kasserver.com [85.13.163.220]) (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 08:53:55 -0700 (PDT) Received: from [192.168.178.26] (dynamic-077-006-101-123.77.6.pool.telefonica.de [77.6.101.123]) by dd44434.kasserver.com (Postfix) with ESMTPSA id 09D805280259 for ; Tue, 17 Oct 2023 17:53:53 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aimeos.com; s=kas202309131159; t=1697558033; bh=q2aqf2OInBFjDSPRSodTtndM8SAqxhPZpml+mCvrWps=; h=Date:Subject:To:References:From:In-Reply-To:From; b=Wz13nK+C2/ePsCKvtQS30iHu3tBWu/CdMtMK1cRF6l/pVjx62Ey8LWUNpkxX/sj/l 1k5DDYmDTim1L0hjgXmQr8OdjHJ8DC4H4PdWwijJ9u8fDwqAXzeVTUb62Nwvc5vPR/ eHGGn4xexQq++A4+zy+JbsiLUO1PRXLX76cxJwxkSg8DK3tfh29KKwf6wOM1N9wsNt B7Jwexk63oztglPEM1ZJPPK67/FYdA8F5n4KE+cYvzpTpJ9RzhNaERhff2ouDgh8Ul fMp9f6UxN3t/4E2SRa5FoFWnYlR8tdvKpD5twMkPEjtfrObgV5TTx3ty+2wmfUHXbB 0bqNSre+J7P7Q== Message-ID: Date: Tue, 17 Oct 2023 17:54:01 +0200 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Content-Language: en-US To: PHP Internals References: Organization: Aimeos GmbH In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Spamd-Bar: ---- Subject: Re: [PHP-DEV] Two new functions array_first() and array_last() From: norbert@aimeos.com (Aimeos | Norbert Sendetzky) On 17.10.23 17:16, Ken Guest wrote: > Having array_value_first and array_value_last to match the existing > array_key_first and array_key_last functions make sense, and would seem to > me to be more intuitive than function names that would not match that > scheme. Please don't make things more complicated than they should be. Functions names should be short and memorable and array_first() and array_last() are perfect. No need for array_value_first() and array_value_last() just for the sake of matching with array_key_first() and array_key_last() functions. Furthermore, returning NULL if the array contains no element is all we need in daily life because most often it doesn't matter if the array is empty or the first value is a NULL value. If it does, checking the array with empty() or using array_filter() is enough. No exceptions, warnings and notices please! Always think of how you would use the functions in your own projects to write short and elegant code, not about all the special cases that should be handled separately. Best, Norbert > On Tue, 17 Oct 2023 at 10:41, Robert Landers > wrote: > >> On Tue, Oct 17, 2023 at 11:37 AM Robert Landers >> wrote: >>> >>> On Tue, Oct 17, 2023 at 11:34 AM Aleksander Machniak >> wrote: >>>> >>>> On 17.10.2023 11:29, Robert Landers wrote: >>>>> $value = array_value_first($array, $key); >>>>> if($key === null) // handle error >>>>> else // do something with $value >>>>> >>>>> You can also freely ignore the key, if you don't need it or care >> about >>>>> 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 = array_key_first($this-array); >> >> // call something that suspends a fiber and results in $this->array >> being mutated >> >> $value = 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 >> hindrance. >> >> Robert Landers >> Software Engineer >> Utrecht NL >> >> -- >> PHP Internals - PHP Runtime Development Mailing List >> To unsubscribe, visit: https://www.php.net/unsub.php >> > > -- Norbert Sendetzky Aimeos GmbH Rennbahnstr. 32 DE-22111 Hamburg E-Mail: norbert@aimeos.com Phone: +49 40 8668 4492 Web: aimeos.com Trade register: District court Hamburg HRB 143090 Managing director: Norbert Sendetzky VAT ID: DE302287839