Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:107049 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 28395 invoked from network); 13 Sep 2019 10:25:24 -0000 Received: from unknown (HELO php-smtp3.php.net) (208.43.231.12) by pb1.pair.com with SMTP; 13 Sep 2019 10:25:24 -0000 Received: from php-smtp3.php.net (localhost [127.0.0.1]) by php-smtp3.php.net (Postfix) with ESMTP id 85AEF2D2011 for ; Fri, 13 Sep 2019 01:01:29 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp3.php.net X-Spam-Level: X-Spam-Status: No, score=-0.5 required=5.0 tests=BAYES_00,BODY_8BITS, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS57367 213.189.55.0/24 X-Spam-Virus: No Received: from cache4.mydevil.net (cache4.mydevil.net [213.189.55.195]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by php-smtp3.php.net (Postfix) with ESMTPS for ; Fri, 13 Sep 2019 01:01:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=korulczyk.pl; s=devil; h=Content-Transfer-Encoding:Content-Type:In-Reply-To :MIME-Version:Date:Message-ID:From:References:To:Subject:Sender:Reply-To:Cc: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=wOo1+4aCkg1ALbDv2kvidI+tNdMvO/l1vVAHKXfyN4g=; b=bVEsRGw4LgwPe1x3u2hVwi9yGM NDvfVpKz1fCwuI9atfJQPTeFtoKuCOPIzruwlGfmGKUaqr428zUEP2EGmi2X4KLlUuCbjghJFk2Ok 6q7IG3NL6VJ8orw825QdxLEc4q2X6B9zOxUypAcmZ2OXFqKnNPaa6wH2BXTwDehL+QxI=; To: Rowan Tommins , PHP internals References: Message-ID: <3c843963-176d-85c8-bf89-cc944640a7fb@korulczyk.pl> Date: Fri, 13 Sep 2019 10:01:24 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.8.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 8bit X-AV-Check: Passed X-System-Sender: robert@korulczyk.pl X-Envelope-From: Subject: Re: [PHP-DEV] [RFC] Reclassifying engine warnings From: robert@korulczyk.pl (Robert Korulczyk) W dniu 12.09.2019 o 22:45, Rowan Tommins pisze: > On 12/09/2019 15:43, Robert Korulczyk wrote: >> One additional line will make your code much more obvious and easier to read and understand: >> >> $i ??= 0; >> $i++; > > > I don't find this code at all obvious: > > foreach ( $something as $foo ) { >     $i ??= 0; >     $i++; > } That is because it does not make sense. You should initialize $i before loop, since it does not need a loop at all (and you probably don't need ??= here): $i ??= 0; foreach ( $something as $foo ) { $i++; } > Even using ??= the initialise-everything-before-use version looks like this: > > $foo = []; > foreach ( $something as $key1 ) { >     foreach ( $somethingElse as $key2 ) { >           $foo[$key1] ??= []; >           $foo[$key1][$key2] ??= 0; >           $foo[$key1][$key2]++; >     } > } Actually you need only one additional line: $foo = []; foreach ( $something as $key1 ) { foreach ( $somethingElse as $key2 ) { $foo[$key1][$key2] ??= 0; $foo[$key1][$key2]++; } } > Again, the values are confusing: the end result will never contain an empty array at the first level, and will never contain a 0 at the second level. It does not look confusing. You have two lines, for two intents - start counting from zero and increment counter on every loop iteration. If one additional line is to much for making your code less ambiguous and more bug-free, then I won't even try to change your mind. Regards, Robert Korulczyk