Shorthand if else and anonymous functions

php

Solution

You can use call_user_func

$str = true ? call_user_func( function(){ return "123"; } ) : 
              call_user_func( function(){ return "321"; } ); 

Problem

I'm trying to use an anonymous function inside a shorthand if else statement. ``` $str = (true) ? function() { //do something return $result; } : function() { //do something else return $result; }; echo $str; ``` This throws a `Object of class Closure could not be converted to string` error since the shorthand if/else stores the function in the $str variable. How can I get the function to execute and then have $str be set to the value that the function return? I know that there is definitely a much easier and simpler way of doing this. But I'm stubborn and I just want my curiosity satisfied.

Original source