-4

Are arrays passed by reference or value in PHP?

For example, let's see this code.

function addWeight($arout, $lineCountry, $lineDomain, $target, $weight)
{
    $currentDomain=getDomain();
    $currentCountry=getCountry();

    if ($currentCountry==$lineCountry && ($currentDomain == $lineDomain || $lineDomain==""))
    {
        $tarobreakpoint=0;
        $arout [$target] =  intval($weight);
    }

    return $arout;
}

Basically it took an array as a parameter. Depending on some circumstances it adds elements to the array. I wonder if this is efficient? If $arout is passed by reference like all arrays should then I think it's efficient. But if it's just copied and passed by value then well it's not.

So what's the verdict?

reformed
  • 4,505
  • 11
  • 62
  • 88
user4951
  • 32,206
  • 53
  • 172
  • 282
  • http://stackoverflow.com/questions/2030906/are-arrays-in-php-passed-by-value-or-by-reference?rq=1 – user4951 Aug 06 '16 at 01:51
  • Possible duplicate of [Are arrays in PHP passed by value or by reference?](http://stackoverflow.com/questions/2030906/are-arrays-in-php-passed-by-value-or-by-reference) – halfer Jan 26 '17 at 11:22

1 Answers1

1

According to the manual, PHP arrays are passed by value:

Array assignment always involves value copying. Use the reference operator to copy an array by reference.

If you'd like to pass an array's reference, use the corresponding operator (&) as mentioned above, and remove the return $arout; line in the addWeight() function:

<?php
// pass $array by reference using & operator
addWeight(&$array, $lineCountry, $lineDomain, $target, $weight);
reformed
  • 4,505
  • 11
  • 62
  • 88