$ provider = function()vs function provider()

I've seen in various coding examples different coding style when creating functions.

What is the difference between creating a function using

$provider = function() { code here }

vs

function provider(){ code here }

Is the first example simply a short version of: $data = provider(); ?
When do we use the first example?

No, it isn't. First code is declaration of closure, i.e. anonymous function. It has no name and can be called with identifier that holds it. Second sample is normal function (user-defined function, to be more specific) and, thus, it will be accessible within all scopes via it's name - not like closure, which will be available for calling only within scope, where it was defined.

You can have as many closures as you wish - they are just callable entities, for example this is valid:

$provider = function() { Code here }
$another  = function() { Code here } //same code

-and calling $provider (for example, with call_user_func()) will have nothing to do with $another

Another significant difference is that closure can accept context parameters like:

$provider = function() use ($param1, $param2, ...) { Code here }

-so inside it's body context parameters will be available. Context parameters are not like usual arguments - since context parameters defined and exist independent from closure while arguments are evaluated at the moment, when call happened.

First declaration is anonymous function.And after assignment,we have variable with name $provider and can call $provider() .Second declaration its just normally function. Anonymous function can be user for example in array_map,array_filter.For example

$a = array(1, 2, 3, 4, 5);
$res = array_filter(
    $a, function ($elem) {
        return $elem > 3;
    }
);
print_r($res);

output element who larger 3