[ACCEPTED]-MySQL ifnull equivalent for php-php

Accepted answer
Score: 13

As of PHP 5.3 you could also use the short ternary operator:

$exTime = get_cfg_var("session.gc_maxlifetime") ?: 1440;

This 7 is basically your anticipated functionality 6 but without having to declare the function. In 5 PHP versions prior to 5.3, you should go 4 with André's answer.

Keep in mind though, that 3 calling the function might throw warnings, if 2 it is about to check arrays in which keys 1 aren't specified:

$array = array(
    0 => array(
        0 => 100
    )
);

$example = isNull($array[0][1], 200);
Score: 3

How about adding this small function?

function isnull($var, $default=null) {
    return is_null($var) ? $default : $var;
}

I don't 4 know of any function that does what you 3 want, but since it's not that hard to implement 2 you might as well do that if you use it 1 a lot.

More Related questions