Reputation: 123
I'm trying to shorten the following. Essentially I want to always get the value of the lefthand as long as $object isn't null, otherwise use a default value.
if ($object) {
$isManual = $object->getIsManual(); // either true or false
} else {
$isManual = true;
}
The following doesn't work since getIsManual can return false which makes it use the righthand.
$isManual = $object ? $object->getIsManual() : true;
Any work arounds? Currently on PHP 7.4.
Upvotes: 0
Views: 474
Reputation: 57121
Just to clarify - the return value of getIsManual()
wouldn't affect the condition, it only evaluates $object
to be 'true' or 'false' (which include null).
You can explicitly check for null in the condition...
$isManual = is_null($object) ? true : $object->getIsManual();
Note that I've reversed the order of the values as I prefer to say is is_null
rather than using a not as well.
Upvotes: 3