I do not commonly use ternary operators, but when I do, I interpret ternary operators as
(condition) ? (do something if condition is true) : (do something if condition is false).
I’d like to use a ternary operator with a function that either returns a value or returns false as the condition, but I want the value to be returned without running the function again in the (do something if condition is true) spot in the ternary operator.
An example:
function getUserName()
{
if (user name is found) {
return $userName
}
return false;
}
what I would like to do:
$my_return_value = (getUserName()) ?: ‘User name not found’;
what I have to do currently:
$my_return_value = (getUserName()) ? getUserName() : ‘User name not found’;
Is there anyway to achieve what I want here without using the getUserName function twice?