I always declare because I don't want to run into undefined vars errors. But I was curious as well. Let's test!
$size = 1000000;
$start_time = microtime(true);
for($i=1;$i<=$size;$i++):
$arr = [];
$arr['foo']='bar';
unset($arr);
endfor;
$results['declare first'] =
floor((microtime(true)-$start_time) * 1000).' milliseconds';
unset($arr, $i, $start_time);
$start_time = microtime(true);
for($i=1;$i<=$size;$i++):
$arr['foo']='bar';
unset($arr);
endfor;
$results['do not declare'] =
floor((microtime(true)-$start_time) * 1000).' milliseconds';
print_r($results);
Typical result on my PC: PHP 5.6.23 on Win 7 x64:
[
'declare first' => '464 milliseconds',
'do not declare' => '376 milliseconds',
]
A 100-millisecond difference after 1 million ops means you basically won't notice the difference unless you have several million arrays. So declare first :-)