-1

For a given dataset - In terms of memory, efficiency and readability - when it is better to use an array, and when it is better to declare multiple variables? What are the pros and cons of using each?

It is - I believe - a fundamental question in programming, and it may have different answers for different programming languages, but the focus is on the PHP point of view (especially regarding the memory management).

EDIT (Example to clarify):

If I have a user, and I want to store his username and password, should I do it in a $user array, or in two variables - $username and $password? What are the pros and cons of each approach?

5
  • 1
    Use array when you need an array. Use variable when you need a variable. Commented Oct 22, 2015 at 10:20
  • @MartinHeralecký But when you can use both - which of them should you use? I will add an example to the question so the question will be clearer. Commented Oct 22, 2015 at 10:22
  • Yes, with example it’d be easier to help. Commented Oct 22, 2015 at 10:26
  • @MartinHeralecký, I added an example. Please read it and contribute an answer. I will really appriciate it. And do not forget to rank up the question. ;) Thank you. Commented Oct 22, 2015 at 10:31
  • Your example isn't appropriate. You can have credentials as separate variables or in array. In this case it doesn't matter. My advice would be: Just write code and when you're not sure if use variable or array, ask there. Commented Oct 22, 2015 at 10:38

1 Answer 1

2

Well, if you want to store more than 1 value in a variable, use array. But if you want to store only one vale in a variable, use normal variable.

EX:

$a = 10;// one value so variable.

Suppose you want to add all multiples of 5 till 20, then use arrays.

$a = array(5,10,15,20);

While storing more than 1 value, if you use differsnt variables, its not a good practice.

$a = 5;
$b = 10;
$c = 15;
$d = 20;

The above mentioned is wrong, you should use arrays.

Finally:

An array is a special variable, which can hold more than one value at a time.

An variable is a normal variable, which can hold only one value at a time.

Irrespective of different languages, concept remains same. It will be up to developer to use array or a normal variable.

EDIT:

As far my understanding, 2 variables will not create any issue with memory.

You can go ahead with variables. If you want to store more values , then use arrays.

But keep in mind that variables are faster that arrays.

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

6 Comments

As of PHP 5.4 you can define arrays using the new method $a = [5, 10, 15, 20];.
I just gave a sample example. But there are many ways of declaring it. But still i accept your suggession.
I edited the question and added an example, please read it and review your answer. And do not forget to rank up the question. ;) Thank you very much!
Thanks for editing. So you are saying that the length of the dataset is a factor in the answer. Well, when it is to big to be in variables? What other factors there are?
see this, stackoverflow.com/questions/14337685/…. even for 5 variables, variables memory is less. It all comes down to our requirement. also see this, stackoverflow.com/questions/4785691/…
|

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.