Does PHP have an equivalent to Python's list comprehension syntax?

Maybe something like this?

$out=array_map(function($x) {return $x*$x;}, range(0, 9))

This will work in PHP 5.3+, in an older version you'd have to define the callback for array_map separately

function sq($x) {return $x*$x;}
$out=array_map('sq', range(0, 9));

PHP 5.5 may support list comprehensions - see the mailing list announcement:

  • [PHP-DEV] List comprehensions and generator expressions for PHP (28 Jun 2012)

And further discussion:

  • What Generators Can Do For You (by ircmaxell; 23 Jul 2012) - has a Fibonacci example.
  • What PHP 5.5 might look like (by NikiC; 10 Jul 2012)
  • Request for Comments: Generators (Wiki started 05 Jun 2012)