On Anonymous function and Closure of PHP


The problem title may be a little big, I would like to ask you the specific question is

$value = Session::get('key', 'default_value');
$value = Session::get('key', function () { return 'default_value';});

The above code means to get the corresponding value according to the key of the session.
When the corresponding value of the key does not exist, the default value (default_value) is returned.

What is the difference between the two writing styles?
The first is naturally understandable.
The second way to return is to use Anonymous function. Is there any specific occasion?
Please, not stint!
Thank you very much!

Ask you a question and you will know when to use Anonymous function:

What if the default value is not known in advance and needs to be calculated afterwards?

Of course, you can also calculate the default value before passing in the second parameter before calling this method, but there will be such a problem. if key exists and no default value is needed, wouldn’t all the codes that were calculated and run by the default value have been wasted? But Anonymous function won’t. Anonymous function’s cuteness is that it will only run when it needs to be called. You just define its running process in advance.

In addition, your topic is a bit inaccurate. The problem mentioned by your body inside has nothing to do with closures.