dukevin
dukevin

Reputation: 23188

Store the return value from an anonymous function into a variable outside its scope

$count = 2;
$amt = 4;
$str = function($count, $amt) {
return "There is ". $count . " and " . $amt;
};
echo $str . "!";

How do I store the return value from the anonymous function into a variable? I know that the entire function itself is being stored into $str which is why this doesn't work, but is there a way to?

Upvotes: 0

Views: 48

Answers (1)

Ronni Egeriis Persson
Ronni Egeriis Persson

Reputation: 2289

You should simply call $str as a function:

echo $str() . "!"

Documentation for anonymous functions as php.net: http://www.php.net/manual/en/functions.anonymous.php

Upvotes: 2

Related Questions