How to pass an array into a function, and return the results with an array

后端 未结 9 1407
你的背包
你的背包 2021-02-05 07:25

So I\'m trying to learn how to pass arrays through a function, so that I can get around PHP\'s inability to return multiple values. Haven\'t been able to get anything to work so

9条回答
  •  滥情空心
    2021-02-05 08:12

    You seem to be looking for pass-by-reference, to do that make your function look this way (note the ampersand):

    function foo(&$array)
    {
        $array[3]=$array[0]+$array[1]+$array[2];
    }
    

    Alternately, you can assign the return value of the function to a variable:

    function foo($array)
    {
        $array[3]=$array[0]+$array[1]+$array[2];
        return $array;
    }
    
    $waffles = foo($waffles)
    

提交回复
热议问题