php null replacement function

php replace special characters
php empty
php is null
php is_null vs null
php is null multiple variables
php null to zero
php date is null
php check value for null

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.

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

NULL - Manual, Attempting to use a void function's return value simply evaluates to NULL , with foreach), as an alternative to the existing list() syntax, which is still supported. NULL without the function call to the constant() function will still retrieve the special type NULL value. Within a class there is no problem, as const NULL="Foo"; will be accessible as myClass::NULL.

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

New features - Manual, If replace has fewer values than search , then an empty string is used for the rest of This function returns a string or an array with the replaced values. In PHP 7, a new feature, null coalescing operator (??) has been introduced. It is used to replace the ternary operation in conjunction with isset () function. The Null coalescing operator returns its first operand if it exists and is not NULL; otherwise it returns its second operand.

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.

str_replace - Manual, This is due to the fact that the empty() function uses __isset() magic functin in these But substitute the line public function __get($var) { return $this->$var; } The is_null function is used to test whether a variable is NULL or not. Version: (PHP 4 and above) Syntax: is_null (var_name) Parameter:

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

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

empty - Manual, is_null — Finds whether a variable is NULL You might want to examine the return value of those functions in detail, but since both are specified to return  PHP | is_null () Function The is_null () function is an inbuilt function in PHP which is used to find whether a variable is NULL or not.

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

is_null - Manual, "login" => NULL "credit" => int(5) } */ ?> Function array_replace "replaces elements from passed arrays into the first array" -- this means replace from top-​right to  PHP str_replace () Function Definition and Usage. The str_replace () function replaces some characters with some other characters in a string. Note: Syntax. Parameter Values. Technical Details. The count parameter was added in PHP 5.0 Before PHP 4.3.3, this function experienced trouble when

array_replace - Manual, return array_map(function(array $array): int { The null coalescing operator (??) It returns its first operand if it exists and is not NULL ; otherwise it returns its expression returns is to replace the spaceship operator with a minus sign (-). use is_null() instead. The first statement misses 0 values. Regards, Calin [Ed. note: this is because == tests for equivalence of value, but not type. NULL evaluates to false, as does 0, so NULL == 0 is true--even though 0 is type int and NULL is type null. You should use either is_null() as noted or ===, which returns true only if its operands are

PHP 7 - Null Coalescing Operator, It is used to replace the ternary operation in conjunction with isset() function. The Null coalescing operator returns its first operand if it exists and is not NULL;  The NVL function allows you to replace null values with a default value. If the value in the first parameter is null, the function returns the value in the second parameter. If the first parameter is any value other than null, it is returned unchanged. We know that COL1 in the test table contains null in all rows except the first.

is_null($x) vs $x === null in PHP, This is_null() function is an inbuilt function in PHP which is used to find whether a variable is NULL or not. It returns True if the given variable is null, otherwise  PHP User Defined Functions. Besides the built-in PHP functions, it is possible to create your own functions. A function is a block of statements that can be used repeatedly in a program. A function will not execute automatically when a page loads. A function will be executed by a call to the function.

Comments
  • Please note that a variable with the value null and a variable that doesn't exist are two different things. Try this: $foo = null; echo $foo; echo $bar;
  • it really surprises me that all this time i thought they were the same. and in my case, null variable isn't the only i want to check, but also the non-existent. thanks for the info.
  • If the variable isset, then it's also not null. isset returns false for null values.
  • @deceze: Oh OK. In that case, you don't need the second version. So are variables that are set to null entirely indistinguishable from non-existing variables within the language (i.e. testing by causing an error is not allowed)?
  • Unfortunately yes. Variables with the value null and non existing variables cannot be distinguished without causing a warning. For arrays you can use array_key_exists, with returns true even for null values.
  • @deceze: Oh, that array lookup maybe even nicer for the present workaround - let me edit that in! Hm, maybe exception handling could be used to track an illegal access, i.e. call a dummy function and see if the call fails because the argument isn't a variable...
  • Warnings are not exceptions. And really, don't overcomplicate this basic thing. If you're accessing non-existing variables, you should get a warning because something's wrong in your logic.