Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:99839 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 92624 invoked from network); 11 Jul 2017 17:35:42 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 11 Jul 2017 17:35:42 -0000 Authentication-Results: pb1.pair.com header.from=ivan.enderlin@hoa-project.net; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=ivan.enderlin@hoa-project.net; spf=permerror; sender-id=unknown Received-SPF: error (pb1.pair.com: domain hoa-project.net from 217.70.183.198 cause and error) X-PHP-List-Original-Sender: ivan.enderlin@hoa-project.net X-Host-Fingerprint: 217.70.183.198 relay6-d.mail.gandi.net Received: from [217.70.183.198] ([217.70.183.198:53277] helo=relay6-d.mail.gandi.net) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id D7/79-47109-C6C05695 for ; Tue, 11 Jul 2017 13:35:41 -0400 Received: from mfilter37-d.gandi.net (mfilter37-d.gandi.net [217.70.178.168]) by relay6-d.mail.gandi.net (Postfix) with ESMTP id 483E5FB887 for ; Tue, 11 Jul 2017 19:35:38 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at mfilter37-d.gandi.net Received: from relay6-d.mail.gandi.net ([IPv6:::ffff:217.70.183.198]) by mfilter37-d.gandi.net (mfilter37-d.gandi.net [::ffff:10.0.15.180]) (amavisd-new, port 10024) with ESMTP id rflV-Gli3_Nj for ; Tue, 11 Jul 2017 19:35:36 +0200 (CEST) X-Originating-IP: 178.211.245.124 Received: from hwhost.local (178-211-245-124.dhcp.voenergies.net [178.211.245.124]) (Authenticated sender: ivan.enderlin@hoa-project.net) by relay6-d.mail.gandi.net (Postfix) with ESMTPSA id D6704FB8CD for ; Tue, 11 Jul 2017 19:35:36 +0200 (CEST) To: internals@lists.php.net References: Message-ID: <3e8e0194-c594-2c3f-4c55-9b21ddfee768@hoa-project.net> Date: Tue, 11 Jul 2017 19:35:35 +0200 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:52.0) Gecko/20100101 Thunderbird/52.2.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-US Subject: Re: [PHP-DEV] array coalesce operator concept From: ivan.enderlin@hoa-project.net (Ivan Enderlin) Hello :-), Thank you for the proposal. I have a question though: How is it different from: * `foreach ($foo ?: [] as $bar) { … }` if `$foo` does not exist, * `foreach ((array) $foo as $bar) { … }` if `$foo` is not an array. I understand your issue, but you can probably type your data with `iterable`, `Generator`, `array`, `Iterator`… Also, I am not sure that `??$x` really means `if (is_array($x)) { … }`. Regards. On 11.07.17 17:02, Mark Shust wrote: > Hello, > > I wanted to garnish feedback on a RFC proposal. This is just a concept at > this point, and is inspired by the null coalesce operator. > > Code will error if a non-array value is passed through a looping feature. > For example, this code: > > > $foo = "abc"; > > foreach ($foo as $bar) { > > echo $bar; > > } > > > will result in the error: > > PHP Warning: Invalid argument supplied for foreach() in test.php on line 3 > > > To prevent this, the logical solution is to wrap this in an is_array check: > > > $foo = "abc"; > > if (is_array($foo)) { > > foreach ($foo as $bar) { > > echo $bar; > > } > > } > > > This code runs successfully and does not error out. For a syntactic > sugar/improvement, this can be shorthand for executing the loop instead of > wrapping the block within an is_array check: > > > > $foo = "abc"; > > foreach (??$foo as $bar) { > > echo $bar; > > } > > > Let me know your thoughts. > > > Cheers, > Mark >