Which of the following two is faster? Only difference being an explicit array() initialization.
$fields['a'] = 1;
$fields['b'] = 2;
vs.
$fields = array();
$fields['a'] = 1;
$fields['b'] = 2;
Which of the following two is faster? Only difference being an explicit array() initialization.
$fields['a'] = 1;
$fields['b'] = 2;
vs.
$fields = array();
$fields['a'] = 1;
$fields['b'] = 2;
Instead of worrying about performance, you should be writing sensible, readable code. This is much better:
$fields = array();
$fields['a'] = 1;
$fields['b'] = 2;
compared to this:
$fields['a'] = 1;
$fields['b'] = 2;
You might save few fractions of a second of a machine; but you will definitely waste valuable time of the person who reads your code. He/she will will have to scroll through your code to locate where $fields is initialized and if it already contains some values.
$fields = array('a' => 1, 'b' => 2);. It saves keystrokes and is easy to understand, IMO.Caution: These numbers vary from hardware to hardware
0.0000109672546386720 seconds without array();
VS
0.0000090599060058594 seconds with array(); (faster!)
But better with array(); Seems more logical.
Micro Benchmark does not make sense just focus on more readable code but for education purpose this is the fastest
$array = array('a' => 1,'b' => 2); // fastest PHP 5.4
$array = ['a' => 1,'b' => 2]; // fastest PHP 5.5