If I'm deep in the nest of loops, I wonder which one is more efficient:
if (!isset($array[$key])) $array[$key] = $val;
or
$array[$key] = $val;
The second form is much more desirable as far as the code is read. Actually, the names are longer, and the array is multidimensional. So the first form ends up pretty rough in my program.
But I wonder if the second form can be slower. Since the code is one of the most frequently executed functions in the program, I would like to use a faster form.
In general, this code will be executed many times with the same value of "$ key". Therefore, in most cases, $ array [$ key] will already be installed, and isset () will return FALSE.
To clarify for those who are afraid that I am processing non-identical code, as if it were identical: as for this part of the program, $ val is a constant. It is unknown before execution, but it is installed earlier in the program and does not change here. Thus, both forms give the same result. And this is the most convenient place to get $ val.
source
share