Happydevdays Happydevdays - 1 year ago 67
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'
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download