<?php
$data = array(3,2,8,5,6,1,7,4,9);
usort($data,
function($a,$b) {
if($a%2==$b%2) return $a>$b;
return ($a%2) ? 1 : -1;
}
);
foreach($data as $v) echo "$v ";
<?php
$getClosure = function($v) {
return function() use($v) {
echo "Hello World: $v!\n";
};
};
$closure = $getClosure(2);
$closure();
<?php
function getMsgFnc() {
global $msg;
return function() use ($msg) {
echo "Msg: {$msg}!<br/>\n";
};
}
$GLOBALS['msg'] = "Hello";
$fnc = getMsgFnc();
$fnc();
$GLOBALS['msg'] = "World";
$fnc();
<?php
function getMsgFnc2() {
global $msg;
return function() use (&$msg) {
echo "Msg: {$msg}!<br/>\n";
};
}
$GLOBALS['msg'] = "Hello";
$fnc = getMsgFnc2();
$fnc();
$GLOBALS['msg'] = "World";
$fnc();