With initialization:
$myArray = array(); if ($myBoolean) { $myArray['foo'] = 'bar'; } return $myArray;
Without initialization:
if ($myBoolean) { $myArray['foo'] = 'bar'; } return $myArray;
In the first case, it turns out what you want if $ myBoolean is false. In the second case, this is not the case, and php may issue a warning when you try to use $ myArray later. Obviously, this is a simplified case, but in the difficult case, the βifβ can be a few lines down and / or not even exist until someone comes and adds it later, without realizing that the array is not initialized.
While not needed, I saw a lack of initialization causing unobvious logical problems like this in complex functions that changed many times over time.
source share