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

Relevant documentation: http://php.net/manual/en/control-structures.foreach.php


3 Answers 3


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.


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.


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

  • No, we don't generally avoid them. We use them where appropriate.
    – N.B.
    May 1, 2012 at 14:27
  • 2
    referenced blog article seems to be simply FUD
    – AD7six
    May 3, 2012 at 14:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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