PHP Functional programming edit

Using outside variables

The use construct is used to import variables into the anonymous function’s scope:

$divisor = 2332;
$myfunction = function($number) use ($divisor) {
    return $number / $divisor;
};

echo $myfunction(81620); //Outputs 35

Variables can also be imported by reference:

$collection = [];

$additem = function($item) use (&$collection) {
    $collection[] = $item;
};

$additem(1);
$additem(2);

//$collection is now [1,2]

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Table Of Contents
4 Functional programming
15 JSON
16 SOAP
18 cURL
20 XML
37 URLs
73 YAML
82 PDO
94 IMAP
103 APCu
109 PSR
  ↑ ↓ to navigate     ↵ to select     Esc to close