Happydevdays Happydevdays - 27 days ago 8
PHP Question

How to simplify php code that checks for null and sets value to replace null?

I have to write a lot of code like this:

if ( !empty($value['fax'])) {
$temp['fax'] = $value['fax'];
} else {
$temp['fax'] = "unknown";
}


just wondering if there's a shorter version of this ...

Answer

Use the ternary operators:

$temp['fax'] = !empty($value['fax']) ? $value['fax'] : 'unknown'