Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - Many variables or One array?

Tags:

I have a question, what is more faster ...

  1. I make many variables that contain all of my data, or
  2. I make one array in one variable that contain all of my data and access it
like image 933
Michael Antonius Avatar asked Jan 15 '13 12:01

Michael Antonius


People also ask

Why arrays are better than variables?

Advantages over variables An array is considered to be a homogenous collection of data. Here the word collection means that it helps in storing multiple values which are under the same variable. For any purpose, if the user wishes to store multiple values of a similar type, an array is the best option that can be used.

What is difference between array and [] in PHP?

Note: Only the difference in using [] or array() is with the version of PHP you are using. In PHP 5.4 you can also use the short array syntax, which replaces array() with [].

How many variables does an array have?

An array variable is one variable; it is not many variables.

How many elements can an array have in PHP?

There is no max on the limit of an array. There is a limit on the amount of memory your script can use. This can be changed in the 'memory_limit' in your php.


1 Answers

Just a try :)

Test 1

With 5 variables

$myvar1 = 'hello'; $myvar2 = 'hello'; $myvar3 = 'hello'; $myvar4 = 'hello'; $myvar4 = 'hello';  print_r(memory_get_usage()); 

Resut : 618600

Test 2

with 5 array keys

$myvar = array(); $myvar['var1'] = 'hello'; $myvar['var2'] = 'hello'; $myvar['var3'] = 'hello'; $myvar['var4'] = 'hello'; $myvar['var5'] = 'hello';  print_r(memory_get_usage()); 

Resut : 620256

like image 198
Dino Babu Avatar answered Dec 06 '22 05:12

Dino Babu