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
Keywords
Related Questions
- What are the potential pitfalls of mixing PHP and HTML styling attributes, and how can they be avoided?
- What are some potential pitfalls to avoid when converting date and time input into a timestamp in PHP?
- What are the best practices for handling file uploads in PHP to avoid errors like "failed to open stream: No such file or directory"?