What's a nicer way to do the following, that doesn't call f() twice?
$x = f() ? f() : 'default';
Setting Default Values for Function parameterPHP allows us to set default argument values for function parameters. If we do not pass any argument for a parameter with default value then PHP will use the default set value for this parameter in the function call. Example: PHP.
An ampersand just before the function name will return a reference to the variable instead of returning its value. Returning by reference is useful when you want to use a function to find to which variable a reference should be bound.
The PHP assignment operators are used with numeric values to write a value to a variable. The basic assignment operator in PHP is "=". It means that the left operand gets set to the value of the assignment expression on the right.
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With