Python-like tuple unpacking for PHP

Python provides a neat way for functions to return multiple arguments via “tuple unpacking”. For example:

def blah:
  return ('one', 'two')

rval_1, rval_2 = blah()

The same can be done in PHP relatively easily via the list construct:

function blah()
{
  return array('one', 'two');
}

list($rval_1, $rval_2) = blah();

Like this article? Please support my writing! Flattr my blog (see my thoughts on Flattr), tip me via PayPal, or send me an item from my Amazon wish list.

Comments

Samat's picture

Unfortunately, this was part of a larger application written in PHP. Why would anyone use PHP if they weren’t stuck with it?

It’s really sort of funky that I apply Python idioms to PHP and Java. I like to think my code is more concise and makes sense to me, but less so to people who only do PHP or Java and not Python… how many times do you see people using PHP’s list construct?