In PHP, we can define functions that return multiple values using an array. Here’s an example demonstrating this concept:
In this example, the vals() function returns an array with two integer values. This is PHP’s way of returning multiple values from a function.
In the main() function, we demonstrate how to use these multiple return values:
We use array destructuring to assign both returned values to variables $a and $b.
We then print these values.
To demonstrate how to use only a subset of the returned values, we use array destructuring again, but this time we ignore the first value by leaving its position empty in the destructuring assignment.
To run this program, save it as multiple_return_values.php and use the PHP command:
While PHP doesn’t have built-in support for multiple return values in the same way as some other languages, using arrays to return multiple values is a common and idiomatic approach in PHP programming.