minte9
LearnRemember



Declaration

Be aware of the ; after function declaration (parse error syntax, without it).
 
/**
 * Anonimous function - declaration
 */

$a = function($var) {
    return "Hello $var";
};  // ; at the end

echo $a('World'); // outputs: Hello World

Callbacks

Anonymous functions are useful when using callback functions.
 
/**
 * Callbaks with filter and map
 */

$A = array(0, 3, 10);

$B = array_filter(
    $A, fn($x) => $x % 2 == 0 // is even
);  

$C = array_map(
    fn($x) => $x*$x, $A // square
); 

print_r($B); // 0, 100
print_r($C); // 0, 9, 100



  Last update: 303 days ago