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

后端 未结 9 1411
你的背包
你的背包 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:04

    I always return multiple values by using a combination of list() and array()s:

    function DecideStuffToReturn() {
        $IsValid = true;
        $AnswerToLife = 42;
    
        // Build the return array.
        return array($IsValid, $AnswerToLife);
    }
    
    // Part out the return array in to multiple variables.
    list($IsValid, $AnswerToLife) = DecideStuffToReturn();
    

    You can name them whatever you like. I chose to keep the function variables and the return variables the same for consistency but you can call them whatever you like.

    See list() for more information.

提交回复
热议问题