If you can get away with relying on truthiness and falsiness instead of the more direct isset
, you can leave out the middle statement in a ternary like this:
$address = $node->field_naam_adres['und'][0]['value'] ?: '';
This works such that if the return value of the first statement evaluates as truthy, that value will be returned, otherwise the fallback will be used. You can see what various values will evaluate to as booleans here
It's important to note that if you use this pattern, you cannot wrap your initial statement in isset
, empty
, or any similar function. If you do, the return value from that statement simply becomes a boolean value. So while the above code will return either the value of $node->field_naam_adres['und'][0]['value']
or an empty string, the following code:
$address = isset($node->field_naam_adres['und'][0]['value']) ?: '';
Will return either TRUE
or an empty string.