16

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.

4 Answers 4

22

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;

Sign up to request clarification or add additional context in comments.

2 Comments

i did this : function if_exist(&$argument, $default = '') { if (isset ($argument)) { echo $argument; } else { echo $default; } $argument = NULL; } but still no luck, result is same as before. :(
@Pawan: This may be possible in your situation, I'm not sure, but what about just making it blank (ie. $argument = '';) and then rather than checking if it's set, check if it's blank. This does, of course, depend on if a blank string is a valid value.
2

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

6 Comments

Assigning to null isn't quite the same as unsetting it, even though it means isset() will give the same result either way. But I guess it'll suffice, since you can't do the same by calling unset() anyway.
i did it too, but still no luck, neither $argument nor $default are being unset. :(
@Pawan Choudhary: Can you show your new code in your original post, and show how you checked whether the variables were set?
function if_exist(&$argument, $default = '') { if (isset ($argument)) { echo $argument; } else { echo $default; } $argument = NULL; } and i'm using the funstion like this : $select = 'something'; if_exist($select, '');
@Pawan Choudhary: The code is very difficult to read in the comments, hence my suggestion that you edit your original post to show your new code. Second, you have not shown how you determine if $select is unset after running if_exists(). You should have a line like if isset( $select) { // do something }.
|
1

the only way to do this with a function is using globals.

//To unset() a global variable inside of a function,
// then use the $GLOBALS array to do so: 

<?php
function foo() 
{
    unset($GLOBALS['bar']);
}

$bar = "something";
foo();
?>

Comments

0

If var is not array, and passed by reference, unset is actually unset the pointer, so it will not affect the original.

However if the var is array, you can unset its keys. eg:

$arr = [
    'a' => 1, 
    'b' => ['c' => 3],
];

function del($key, &$arr) {
    $key = explode('.', $key);
    $end = array_pop($key);
    foreach ($key as $k) {
        if (is_array($arr[$k]) {
            $arr = &$arr[$k];
        } else {
            return; // Error - invalid key -- doing nothing
        }
    }
    unset($arr[$end]);
}

del('b.c', $arr); // $arr = ['a' => 1, 'b' => []]
del('b',   $arr); // $arr = ['a' => 1]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.