Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:102326 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 80801 invoked from network); 20 Jun 2018 14:24:38 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 20 Jun 2018 14:24:38 -0000 Authentication-Results: pb1.pair.com header.from=rowan.collins@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=rowan.collins@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 74.125.82.43 as permitted sender) X-PHP-List-Original-Sender: rowan.collins@gmail.com X-Host-Fingerprint: 74.125.82.43 mail-wm0-f43.google.com Received: from [74.125.82.43] ([74.125.82.43:39009] helo=mail-wm0-f43.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 54/B5-32156-5A36A2B5 for ; Wed, 20 Jun 2018 10:24:38 -0400 Received: by mail-wm0-f43.google.com with SMTP id p11-v6so41767wmc.4 for ; Wed, 20 Jun 2018 07:24:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=iqHz4PT5dFDiBuP+6iGaT3GOm9hrbNomZdyOcX0y05A=; b=sDQMjP+156H6o7kxD8YFpRABaoaE+O++VAuSYQ9mL2WegY39D2EIGIeSOOJb3MPU4V 2uTQyjkUJfI8Sm1FPlsH4IuhQtHCdd+AjH30ogU1+p8jEuj4Bc8RttqhyF+GAsBXQa6t jqOVGvKOmeZVGDCPxJPTsdVGytMynLAs4CEsrZlHTfpKoFQoBRbDFBC5fNSE4oBn5dqc azl4SBVaA9fOKt/d6YqZr8kZx+cc+qQCJlBUUhQR1i2n8CxQ79SeM7uRKMSUe5Z61/nt fczpy8b4ZwoR42hSEsYaqrJBHo5zcJCw0Apapn8ValyLw+79UldLwZiaprUfN6IhuV8Z N95w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=iqHz4PT5dFDiBuP+6iGaT3GOm9hrbNomZdyOcX0y05A=; b=AfSN1QvCHdRQPzq6CgdLixN5mIsmwstr9g5B6/RY/C4EalEkeO27WqKaeY+dbOHroI asaGLSCjVN32jkhOZ8zd+PrGdHqu9el3cJyNREmOfcg5xj6AT+qYW1FP0u7zWBONypHI tKmbG3j0pNRltXacZNGNY5ISjcfzgRiOB5GDEft/HWuDibWui4b3nekz/8vB+l2vtoki Llcl5tem69005u6/Fc5DqwBi0qBiw8p4owXzVFG79QIdfkGhgDY7eyAnflNiMnImvgYD qY0Kw1D/H3KqEY555hTVTmOPLBLnjTMnP0nplHeapJCQoaeRaDn+hTUFDEsj/T4FK/9q P/Cg== X-Gm-Message-State: APt69E2nPaDNuGy3sXJKsCopXrq2hx0Cr5tLBcUUekwzGS/FZpVZj/Pu /9lTKwWA/u6WQaGIStbaXT0x2F7NjvYezQnCsSM= X-Google-Smtp-Source: ADUXVKInrDLCXwGnVkWFk6PuJXr0UwyyECO952FNUj1O/Pzuv3mduZyZbrf7oA7NpsNLXbqGn1e3uEltAfRdun8wAu8= X-Received: by 2002:a50:d08e:: with SMTP id v14-v6mr18391201edd.182.1529504675246; Wed, 20 Jun 2018 07:24:35 -0700 (PDT) MIME-Version: 1.0 Received: by 2002:a50:8d55:0:0:0:0:0 with HTTP; Wed, 20 Jun 2018 07:24:34 -0700 (PDT) In-Reply-To: References: <75db877c-ef0b-7d3b-48a8-9879d90352b0@web.de> <76e4744f-6722-ee49-0325-e4433236f1b1@web.de> <73ac8914-8c9f-e74b-a4f5-f6e39cd96f8f@gmx.de> <3242366.BL9MUqXe46@vulcan> <154ba124-2915-ca36-fa39-5df6726d3b5a@web.de> Date: Wed, 20 Jun 2018 15:24:34 +0100 Message-ID: To: Levi Morrison Cc: enno.woortmann@web.de, internals Content-Type: multipart/alternative; boundary="000000000000d9b57a056f138ed0" Subject: Re: [PHP-DEV] [RFC][Under Discussion] Add functions array_key_first()and array_key_last() From: rowan.collins@gmail.com (Rowan Collins) --000000000000d9b57a056f138ed0 Content-Type: text/plain; charset="UTF-8" On 20 June 2018 at 15:10, Levi Morrison wrote: > On Wed, Jun 20, 2018 at 8:09 AM Rowan Collins > wrote: > > > > On 20 June 2018 at 14:55, Levi Morrison wrote: > >> > >> > >> if ([$key, $value] = array_last($input) { > >> // do something > >> } > >> > > > > > > Unfortunately, this won't work. I didn't know list() could even be used > as an expression, but apparently if it is, it evaluates to the whole array > on its right-hand side, which makes sense. > > > > So, if array_last([]) returns [null, null], then [$key, > $value]=array_last([]) will also evaluate to [null, null]. Since that's not > an empty array (it has two elements in it), it will evaluate to true. > https://3v4l.org/lIEa1 > > > > If, on the other hand, array_last([]) returned something empty, like [], > the list() assignment would throw Notices for the implicit access to > elements [0] and [1] of the empty array. https://3v4l.org/qhq5R > > It returns `?array`. So either `null` or `[$key, $value]`. There are > no notices; sorry for the lack of detail. > Ah, OK, you learn something every day! For anyone else like me who was unaware of this, list() apparently silently accepts any non-array value on the right of the assignment, assigns all variables in the list() to null, and evaluates to the assigned (but otherwise unused) value. So $x=list($a,$b)=42; will set $a and $b to NULL, and $x to 42, without any notices. I don't think I would ever have expected that behaviour, but apparently it's been that way forever, so I guess it's fine to exploit it. Incidentally, there's no mention of this on the manual page, which is also missing information on the short-hand syntax and key-based destructuring introduced in PHP 7.1: http://php.net/list Regards, -- Rowan Collins [IMSoP] --000000000000d9b57a056f138ed0--