PHP doesn’t support to return multiple values in a function. Inside a function when the first return statement is executed, it will direct control back to the calling function and second return statement will never get executed. However, there are ways to work around this limitation.
The multiple values can be returned from a function by using an array.
Example 1: This example shows how to return multiple values from a function in PHP. First, create an empty array end push the element to the array and then return the array.
Factors of 24 are:
Example 2: This example uses list function to store the swapped value of variable. It is used to assign array values to multiple variables at the same time. The multiple values returned in array from the function can be assigned to coresponding variables using list().
Before swapping the elements
a = 10
b = 20
After swapping the elements
a = 20
b = 10
- How to get multiple selected values of select box in php?
- PHP | Ds\Map values() Function
- D3.js | d3.map.values() Function
- D3.js | d3.set.values() Function
- D3.js | d3.values() Function
- Mapping external values to dataframe values in Pandas
- What does main() return in C and C++?
- Multiple Inheritance in PHP
- CSS | Multiple Columns
- Return all dates between two dates in an array in PHP
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.