currently i'm using this php function :
function if_exist(&$argument, $default = '')
{
if (isset ($argument))
{
echo $argument;
}
else
{
echo $default;
}
}
i want this function to unset the variables $argument(passed by reference) and $default just after echoing their value, how can i do this? Thanks in advance.
PHP | unset() Function The unset() function is an inbuilt function in PHP which is used to unset a specified variable.
unset() destroys the specified variables. The behavior of unset() inside of a function can vary depending on what type of variable you are attempting to destroy. If a globalized variable is unset() inside of a function, only the local variable is destroyed.
Introduction. In PHP, arguments to a function can be passed by value or passed by reference. By default, values of actual arguments are passed by value to formal arguments which become local variables inside the function. Hence, modification to these variables doesn't change value of actual argument variable.
if you see in php documentation there are not available directly remove multiple keys from php array. But we will create our own php custom function and remove all keys by given array value. In this example i created custom function as array_except().
According to the manual for unset:
If a variable that is PASSED BY REFERENCE is unset() inside of a function, only the local variable is destroyed. The variable in the calling environment will retain the same value as before unset() was called.
I assume this is the issue you're encountering. So, my suggestion is to simply set $argument
to NULL
. Which, according to the NULL docs will "remove the variable and unset its value.".
For example:
$argument = NULL;
$default
is passed by value, so it cannot be unset (except in the local scope).
As you undoubtedly discovered, unset()
simply unsets the reference to $argument
. You can (sort of) unset $argument
like this:
$argument = null;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With