How can you return multiple values as an array from a PHP function?
To return multiple values as an array from a PHP function, you can simply create an array within the function and populate it with the values you want to return. Then, return this array at the end of the function. This allows you to easily return multiple values in a structured way.
function getMultipleValues() {
$value1 = 'First value';
$value2 = 'Second value';
$value3 = 'Third value';
$valuesArray = array($value1, $value2, $value3);
return $valuesArray;
}
// Call the function and store the returned array
$returnedValues = getMultipleValues();
// Access the values in the returned array
echo $returnedValues[0]; // Output: First value
echo $returnedValues[1]; // Output: Second value
echo $returnedValues[2]; // Output: Third value