How can arrays be used effectively to return multiple values from a PHP function?

When a PHP function needs to return multiple values, using an array is an effective solution. By storing the values in an array and returning that array from the function, you can easily access all the values in the calling code. This approach simplifies the code and makes it more maintainable compared to returning multiple values separately.

function getMultipleValues() {
    $value1 = 'First value';
    $value2 = 'Second value';
    
    return array($value1, $value2);
}

// Calling the function and accessing the returned values
$result = getMultipleValues();
echo $result[0]; // Output: First value
echo $result[1]; // Output: Second value