dqiu
dqiu

Reputation: 337

php null replacement function

there is this function called Nz() in visual basic for application. the function checks variable nullity and returns a provided value if it finds the variable is null.

i try to write the same function in php, which looks like below:

function replace_null($value, $replace) {
    if (!isset($value)) {
        return $replace;
    } else {
        return $value;
    }
}

$address = replace_null($data['address'], 'Address is not available.');

of course, if $data['address'] is found null, php will stop executing the code and replace_null won't be called.

i'm currently using ternary

(isset(data['address']) ? data['address'] : 'Address is not available.');

but i think replace_null, if it works, will offer a more convenient way.

is there a function in php that provide the same functionality as vba's Nz()? any suggestion will be appreciated.

thanks in advance.

Upvotes: 1

Views: 17917

Answers (7)

Buddhimal Gunasekara
Buddhimal Gunasekara

Reputation: 76

function replace_null($value='', $replace='') {
    if (!isset($value)) {
        return $replace;
    } else {
        return $value;
    }
}

Try this. It will allow you to call the function with or without passing parameters.

Upvotes: 1

user1272855
user1272855

Reputation: 82

If the variable is declared you do something like $value?:$replace

Upvotes: 1

Soylent17
Soylent17

Reputation: 161

If you pass by reference, PHP won't error out:

function Nz(&$var, $def='') {
    return isset($var) ? $var : $def;
}

http://php.net/manual/en/language.references.pass.php

Upvotes: 1

MECU
MECU

Reputation: 780

I think using the is_null function would be much more useful:

$address = $data['address'];
if ( is_null($address) ) $address = 'Address is not available.';

If you really want this as a function:

function replace_null($value, $replace) {
    if (is_null($value)) return $replace;
    return $value;
}
$address = replace_null($data['address'], 'Address is not available.');

Upvotes: 0

Kerrek SB
Kerrek SB

Reputation: 477060

A bit roundabout: If you only use this to check for array members, you could pass the key separately:

function get_with_default($arr, $key, $defval)
{
  return isset($arr[$key]) ? $arr[$key] : $defval;
}

If the variable could be set but null (Edit: which it cannot, thanks @deceze), add another check:

function get_and_coalesce_with_default($arr, $key, $defval)
{
  return (isset($arr[$key]) && !is_null($arr[$key]) ? $arr[$key] : $defval;
}

As pointed out, isset() only succeeds on non-null values, so the above doesn't add anything. We can write a non-trivial check with array_key_exists, though:

function get_with_default_v2($arr, $key, $defval)
{
  return (array_key_exists($key, $arr) && !is_null($arr[$key]) ? $arr[$key] : $defval;
}

Upvotes: 3

Clint C.
Clint C.

Reputation: 688

<?
function replace_null($value, $replace) {
    if(empty($value) && $value !== '0') {
        return $replace;
    } else {
        return $value;
    }
}

$address = replace_null("0", "replacing null");
echo $address;
?> 

Upvotes: 0

Jonah
Jonah

Reputation: 10091

You could pass the array and the key separately like this:

function valueOrDefault($array, $key, $default) {
    return isset($array[$key]) ? $array[$key] : $default;
}

$address = valueOrDefault($data, 'address', 'Address is not available.');

Upvotes: 1

Related Questions