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
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php