Yong Quan Yong Quan - 3 months ago 44
PHP Question

Is it better to get values from array or declaring them each time?

Example 1:

$a = 'value1';
$b = 'value2';
$c = 'value3';

$x = 'value1';
$y = 'value2';
$z = 'value3';

Example 2:

$var = array('value1', 'value2', 'value3');

$a = var[0];
$b = var[1];
$c = var[2];

$x = var[0];
$y = var[1];
$z = var[2];

Which example has less server resource usage? Which example is faster?


In your neither has any impact on speed as it is trivial. If we talk about memory usage, then array uses more as it stores the same variables + reffence links (again, trivial but that is how it works). But there as other factors that you need to consider:

1.Readability - if you use array approach you code will be very dificult to trace especially in bigger functions.

2.Operations - if you use array array approach your operations with variables will be limited as things like += will break the array refference and store the values directly in variable.

3.Relations with other devs - if you use the array approach anyone else that has to work with your code will be spilling curses your way ;)