3

I have abit of a silly question. I was just wondering. Whilst coding im sometimes tend to, if I have lot of variables that relate to a specific entity, create an associative array using key value pairs to define these entities.

What Id like to know is, Im aware that they all get saved in memory but which method is smaller/faster, creating a few variables or creating an array with keys and values of the variables

Below are some examples:

$apples  = 'apples'; 
$grapes  = 'some grapes';
$bananas = 'lots of bananas';

$fruits = ['apples' => 'apples', 'grapes' => 'some grapes', 'bananas' => 'lots of bananas'];

What I'll be using this for is looping over entries from the database and defining values to populate in my markup.

7
  • It's relating to what do you want to do after. Commented Feb 17, 2018 at 6:55
  • You should check this: stackoverflow.com/a/14337876/1485183 Commented Feb 17, 2018 at 6:56
  • 3
    The difference in speed or memory consumption is irrelevant, don't worry about it. Worry about requests to remote servers, database queries, file-system access. Commented Feb 17, 2018 at 6:57
  • Thanks for the speedy response, ill update my question quick Commented Feb 17, 2018 at 6:59
  • 1
    If you have related values to store, an array probably makes good sense. If you want to iterate the values, then an array is the way. Commented Feb 17, 2018 at 7:07

3 Answers 3

3

Speed and memory are likely to be irrelevant. Write clean, direct code.

If you are going to be iterating or searching these values, use an array.

As a fundamental rule, I don't declare single-use variables. Only in fringe cases where readability is dramatically improved do I break this rule.

Sign up to request clarification or add additional context in comments.

3 Comments

These values are populated inside of the loop though, is that that a good fit for the array rather than a variable?
I say yes. Using arrays is going to result in more concise, easier to read, and easier to maintain code. This I say generally. I'd have to see actual code to confirm. Another point is: don't declare single-use variables.
Thanks @mickmackusa much appreciated!
2

Lets Try

Test 1

With 6 PHP variables

$makevar1 = 'checkspeed';
$makevar2 = 'checkspeed';
$makevar3 = 'checkspeed';
$makevar4 = 'checkspeed';
$makevar5 = 'checkspeed';
$makevar6 = 'checkspeed';

print_r(memory_get_usage()); 

Result is 458056

Test 2

With 6 array keys

$makevar = array();
$makevar['var1'] = 'checkspeed';
$makevar['var2'] = 'checkspeed';
$makevar['var3'] = 'checkspeed';
$makevar['var4'] = 'checkspeed';
$makevar['var5'] = 'checkspeed';
$makevar['var6'] = 'checkspeed';

print_r(memory_get_usage());

Result is 459168

Final Result: Accessing a variable is faster than array.

1 Comment

Thanks for the reply. Technically your test shows that variables result in less memory usage rather than speed of use.
1

Using the array in PHP could possibly be slower than variables. However, it's not worth looking into. Focus on readability instead.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.