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 <landers.rob...@gmail.com>
wrote:

On Tue, Oct 17, 2023 at 11:37 AM Robert Landers
<landers.rob...@gmail.com> wrote:

On Tue, Oct 17, 2023 at 11:34 AM Aleksander Machniak <a...@alec.pl>
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: norb...@aimeos.com
Phone:  +49 40 8668 4492
Web: aimeos.com

Trade register: District court Hamburg HRB 143090
Managing director: Norbert Sendetzky
VAT ID: DE302287839

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: https://www.php.net/unsub.php

Reply via email to