php null replacement function

自作多情 提交于 2020-01-05 12:08:31

问题


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.


回答1:


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;
}



回答2:


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.');



回答3:


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.




回答4:


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

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



回答5:


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.');



回答6:


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




回答7:


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



来源:https://stackoverflow.com/questions/7101820/php-null-replacement-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!