PHP supports the list() language construct which, in short, allows you to return multiple values from a function and then attach them to different variable, eg:

function myBigReturn(){
    return array("foo", "bar");
list($fooer, $barer) = myBigReturn();
echo $fooer; // echoes "foo"
echo $barer; // echoes "bar"

I have failed to find much info about such a language construct and I am curious - is usage of PHP list() construct considered a bad coding convention? Are there any serious articles/literature on this subject?

  • 2
    Why would it be a "bad coding convention"? Just because a function, technique, concept doesn't exist on another language, it doesn't mean there's something wrong with it. Also: PHP list() equivalent in Python.
    – yannis
    Jul 23, 2013 at 11:34
  • @YannisRizos I am not saying it's a bad convention, I am simply asking. The question stems from the fact that I want to leave a manageable code behind me, and I am curious if list() is widely accepted as good or bad code and thus can affect the readability for further generations of programmers.
    – Maurycy
    Jul 23, 2013 at 12:41
  • Every structure / technique / practice can become a bad convention, if sufficiently abused. If you use it for a handful of values, as mhoran_psprep suggests, you shouldn't have any problems.
    – yannis
    Jul 23, 2013 at 13:33
  • Another option for handling multiple return values is to return an instance of stdClass, with the multiple values you want to send back set as properties in that object. A good example of that is fetchObject() in PDO. Jul 23, 2013 at 17:42

1 Answer 1


I dare say it is almost pythonic. There is a similar construct in Python and very useful for automatically splitting out the return values.

The place I have seen it the most is when a function returns a point, which has an x/y, latitude/longitude, or azimuth and range value. At times a function can return a object with defined fields, but sometimes the automatic splitting is just what is needed.

As Rath points out there is no real speed difference.

It is generally not used when the number of items in the array is more than a handful, or if there is not a fixed number of items being returned.

  • Also good for exploding dates: list($year, $month, $day) = explode('-', $isodate); or the matches of preg_match: if (preg_match('/(.).(.)/', $text, $matches)) { list (, $first, $second) = $matches; } (notice the first comma to avoid $matches[0]. As list is a language construct and not a function, you can do that). Jul 23, 2013 at 13:50
  • Well, even if "pythonic" makes sense applied to things which aren't Python, it isn't necessarily good in the context of the whole design. I say this despite liking Python very much and PHP not so much.
    – user7043
    Jul 23, 2013 at 15:01
  • Mind: x/y, latitude/longitude, ... in PHP will typically use associative arrays (dictionaries in other environments). With code like function getCoords() { return ['long' => 48.13, 'lat' => 11.57];} list($long, $lat) = getCoords(); the caller depends on implementation details - the order in which the elements were added, not the names.
    – johannes
    Jul 23, 2013 at 15:54
  • It's not pythonic...I suspect its more Perlonic, as such behavior is available in Perl, and early PHP was largely derived from Perl. Jul 23, 2013 at 17:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.