Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:99869 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 27524 invoked from network); 13 Jul 2017 09:55:50 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 13 Jul 2017 09:55:50 -0000 X-Host-Fingerprint: 62.31.75.76 76.75-31-62.static.virginmediabusiness.co.uk Received: from [62.31.75.76] ([62.31.75.76:17869] helo=localhost.localdomain) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id C3/DC-01782-4A347695 for ; Thu, 13 Jul 2017 05:55:48 -0400 Message-ID: To: internals@lists.php.net References: In-Reply-To: Date: Thu, 13 Jul 2017 10:55:44 +0100 Lines: 1 MIME-Version: 1.0 Content-Type: text/plain; format=flowed; charset="utf-8"; reply-type=original Content-Transfer-Encoding: 7bit X-Priority: 3 X-MSMail-Priority: Normal Importance: Normal X-Newsreader: Microsoft Windows Live Mail 16.4.3564.1216 X-MimeOLE: Produced By Microsoft MimeOLE V16.4.3564.1216 X-Posted-By: 62.31.75.76 Subject: Re: [PHP-DEV] array coalesce operator concept From: TonyMarston@hotmail.com ("Tony Marston") "David Rodrigues" wrote in message news:CAEsg9X2ECG62i7Z_Nu11kqr7yVBkmUcd3MxGt78ULAmpFx--_w@mail.gmail.com... > >The idea is good, but I don't know if it is more clear than an 'if()' I think that this proposal is totally ridiculous and should be shot down in flames. My reasoning is as follows: 1) There should be a rule that anything which can be achieved with 5 lines or less in userland code should be automatically excluded from becoming a change to the language. The cost of changing the language in order to get around the laziness of a small number of users versus the "benefit" of making the language more complicated for the rest of us, as well as adding to the maintenance burden is simply not worth it. 2) Proposals such as this violate the first rule of good programming which is to provide code which is readable and therefore understandable by others, and therefore maintainable. I don't know about you, but I find it much easier to read words than symbols. The idea that the code "if (is_array($foo))" is too verbose and should be replaced with a shorthand symbol is just too ridiculous for word. If any more of these shortcuts is implemented then I'm afraid that code will look too much like a bunch of hieroglyphics and become far less readable. Just my 2 cents worth. -- Tony Marston >Em 11 de jul de 2017 12:15 PM, "Mark Shust" escreveu: > >> 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 >>