Happydevdays Happydevdays - 9 months ago 51
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 Source

Use the ternary operators:

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