Aside from readability, are there any pros/cons to either of the following examples. The first i will access the value using the array index each time i use it and the second example i will assign to a temporary varaiable and then just use the variable
$myArray = ['value_1' => 'value 1', 'value_2' => 'value 2', 'value_3' => 'value 3'];
$value1 = $myArray['value_1'];
// Do lots of things with $value1
Versus
$myArray = ['value_1' => 'value 1', 'value_2' => 'value 2', 'value_3' => 'value 3'];
// Do lots of things with $myArray['value1']
http://sandbox.onlinephpfunctions.com/code/c5acd07edcc910bfdd80a27bee0583d8540e17e3
Here is the result by doing it 100000 times:
First: 0.0018520355224609 with a pointer to the actual value
Second: 0.003324031829834 with a pointer to the array
Like you can see there is a big difference but now computers are to fast to even make it worth your time worrying about it.
This entirely depends on your intention. If you ever only read from it, there's no functional difference. If you write to the value as well, there probably will be a functional difference.
Assuming that in your case there is no functional difference, the array access should be ever so slightly more expensive than the direct variable access. However, this does not mean you should bend over backwards to optimise for this case. The difference in performance will be so minuscule as to be hardly measurable in real world applications. So readability should be your only deciding factor.
Using named keys is slower than numeric keys is quite slower than using a scalar variable:
<?php
$myArray = ['value_1' => 'value 1', 'value_2' => 'value 2', 'value_3' => 'value 3'];
$myArray2 = ['value 1', 'value 2', 'value 3'];
$tt = microtime(true);
for ($i = 0; $i<1e7; $i++) $x = $myArray['value_1'];
$dt = microtime(true)-$tt;
echo $dt.PHP_EOL;
$tt = microtime(true);
for ($i = 0; $i<1e7; $i++) $x = $myArray2[0];
$dt = microtime(true)-$tt;
echo $dt.PHP_EOL;
$tt = microtime(true);
$y = $myArray['value_1'];
for ($i = 0; $i<1e7; $i++) $x = $y;
$dt = microtime(true)-$tt;
echo $dt.PHP_EOL;
?>
gives:
0.80559897422791 // named key
0.74200701713562 // numeric key
0.54049301147461 // scalar
Multiple times dereferencing an array in a loop with many iterations can therefore be costly. But that applies primarily to number-crunching and stuff like that, where PHP is basically not the best option, being not designed for it.
EDIT Interesting enough, assigning an integer value from a variable is about 10-20% faster than from a scalar integer on Xeon processors, whereas on a Intel i5 machine there is no difference. The code here gives the following results on a Xeon box:
Accessing associative array 0.900
Accessing numerical array 0.794
Assigning from reference to associative array 0.662
Assigning a reference to an associative array element 0.877
Assigning scalar variable 0.499
Assigning scalar string 0.659
Assigning scalar integer 0.562
But this may vary depending on code size due to register usage.