Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is it considered bad practice to pass items in a PHP array by reference instead of by value?

Relevant documentation:

share|improve this question
Be wary. – animuson May 1 '12 at 3:39
up vote 5 down vote accepted

It's not harmful if it's appropriate for the use case.

If you are concerned:

foreach($foo as &$bar) {
    $bar['addthis'] = 'x'; 

Can also be written as:

foreach($foo as $i => $bar) {
    $foo[$i]['addthis'] = 'x'; 

Note that as indicated by animuson in a comment, and the big fat warning on the docs page, if you leave a reference lying around and reuse the variable name - you're going to update what you are referencing.

That doesn't mean references are harmful; it just means learn how to use a tool - and then use it correctly.

share|improve this answer

No, it isn't considered bad practice. If it was bad in any way, then it wouldn't exist in the first place.

What is bad is when programmers misuse references or use them where they aren't needed.

You cannot answer simply whether it's bad or good to use or avoid references. You'll always get a few guys in "don't use references" camp that will provide a disaster scenario where your code blows up, and then you'll have another camp of people showing benchmark results with and without references.

Bottom line: use them where appropriate. Don't rely on random blogs that invent a bogus scenario where references are bad, especially if that scenario has no real world application.

share|improve this answer

References are a way to have multiple variables referencing the same variable container using different names -- so whatever name you're using an operation on that variable will always have an effect on the others.So generally we avoid References.For details see a link
Do not use PHP references

share|improve this answer
No, we don't generally avoid them. We use them where appropriate. – N.B. May 1 '12 at 14:27
referenced blog article seems to be simply FUD – AD7six May 3 '12 at 14:20

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.