Re: PATCH: anonymous functions in PHP

From: Date: Mon, 19 Mar 2007 02:06:41 +0000
Subject: Re: PATCH: anonymous functions in PHP
References: 1  Groups: php.internals 
Request: Send a blank email to internals+get-28406@lists.php.net to get a copy of this message
$data = array("zoo", "orange", "car", "lemon", "apple"); usort($data, function($a, $b) { return strcmp($a, $b); }); var_dump($data); # data is sorted alphabetically
What happens if you do this: $data = array("zoo", "orange", "car", "lemon", "apple"); $rev = 1; usort($data, function($a, $b) { return $rev?strcmp($a, $b):!strcmp($a, $b); }); var_dump($data); # data is sorted alphabetically This works in Javascript (probably Ruby too), but quite hard to make work in PHP because $rev is in different scope. Moreover, would it mean that this: $f = function($a, $b) { return $rev?strcmp($a, $b):!strcmp($a, $b); } would work too? Keeping right value of $rev? -- Stanislav Malyshev, Zend Products Engineer stas@zend.com http://www.zend.com/

« previous php.internals (#28406) next »