$applicantName = (isset($params['applicant']['name']) && $params['applicant']['name'] != '') ? $params['applicant']['name'] :
((isset($simulationCustomer['fullname']) && $simulationCustomer['fullname'] != '') ? $simulationCustomer['fullname'] :
($simulationCustomer['name'] ?? ''));
I was this code today and of course it doesn't feel right. In JavaScript it would be so much easier to write, the problem in PHP is that the coalesce operator works for null values only.
Is there a way to write this in a more understandable way?
What about to use array_filter
?
$applicantName = array_values(array_filter([
$params['applicant']['name'] ?? '',
$simulationCustomer['fullname'] ?? ''
]))[0] ?? '';
array_values
is only for reseting index key.
Or
$names = array_filter([
$params['applicant']['name'] ?? '',
$simulationCustomer['fullname'] ?? ''
]);
$applicantName = reset($names) ?: '';