My solution is slightly longer than Shauns' but I thought I'd throw it up anyway. It should work identically to the original syntax, even in error cases. I'm basically exploiting the ternary syntax to allow two lines in one. I also changed the temporary variable to ${0}
instead of ${''}
since it save a character and variables that begin with numbers are not valid.
The below statements,
line1;
$output = line2;
Is identical to the following statement for every possible case.
$output = (line1)&&0?:(line2);
My solution:
'hello'); }
$key = 0;
$output = (${0}=func_return_array())&&0?:${0}[$key];
echo '1: ' . $output . "\n";
// 2
class Thing {}
$class_base = 'Thi'; $class_suffix = 'ng';
$output = (${0}=$class_base.$class_suffix)&&0?:new ${0};
echo '2: ';
var_dump($output);
// 3
$func_base = 'func_'; $func_suffix = 'return_array';
$output = (${0}=$func_base.$func_suffix)&&0?:${0}();
echo '3: ';
var_dump($output);
// 4
function func_return_closure() {
return function() {
return 'This is a closure';
};
}
$output = call_user_func(func_return_closure()); //more straight forward
//$output = (${0}=func_return_closure())&&0?:${0}();
echo '4: ';
var_dump($output);
?>