How can PHP be used to return multiple values in a function?
In PHP, a function can only return a single value. To return multiple values from a function, you can use an array or an object to encapsulate all the values and return that instead. This allows you to effectively return multiple values in a structured way.
function getMultipleValues() {
$value1 = 10;
$value2 = 'Hello';
$values = [$value1, $value2];
return $values;
}
// Usage
list($result1, $result2) = getMultipleValues();
echo $result1; // Output: 10
echo $result2; // Output: Hello
Keywords
Related Questions
- How can the use of references in foreach loops impact the accessibility and manipulation of values in associative arrays in PHP?
- How can Docker containers be utilized for PHP development with frameworks like Phalcon?
- Are there any best practices for ensuring consistent display of input elements across different browsers in PHP?