Assignment to variables

Download php eBook

Example

Anonymous functions can be assigned to variables for use as parameters where a callback is expected:

$uppercase = function($data) {
    return strtoupper($data);
};

$mixedCase = ["Hello", "World"];
$uppercased = array_map($uppercase, $mixedCase);
print_r($uppercased);

These variables can also be used as standalone function calls:

echo $uppercase("Hello world!"); // HELLO WORLD!

Stats

Contributors: 3
2016-07-22
Licensed under: CC-BY-SA

Not affiliated with Stack Overflow
Rip Tutorial: info@zzzprojects.com

Download eBook