What\'s a nicer way to do the following, that doesn\'t call f() twice?
$x = f() ? f() : \'default\';
In PHP 5.3, you can also do:
$a = f() ?: 'default';
See the manual on ?: operator.
function f()
{
// conditions
return $if_something ? $if_something : 'default';
}
$x = f();
This seems to work fine:
$x = f() or $x = 'default';
You could save it to a variable. Testcase:
function test() {
echo 'here';
return 1;
}
$t = test();
$x = $t ? $t : 0;
echo $x;
$x = ($result = foo()) ? $result : 'default';
test