1

I have a PHP function:

function unserialize_recursive($data, $i = 0) {

    $unserialized = unserialize($data);

    if ($unserialized) {
        $i++;
    }

    if (!is_string($unserialized) || unserialize($unserialized) === FALSE) {
        /* placeholder - see explanation below */
        return array($i, $unserialized);
    } elseif (unserialize($unserialized) !== FALSE) {
        unserialize_recursive($unserialized, $i);
    }
    return FALSE;
}

I call this function with:

$data = unserialize_recursive($serialized_string);
var_dump($data);

But the output of var_dump($data) is bool(false).

However, if I add var_dump($unserialized) in the position of the text /* placeholder - see explanation below */, I get the expected output.

So why can I not return that variable ($unserialized)? If I use gettype() on it at that point in the function, it returns array.

I'm using Netbeans and all the syntax highlighting indicates the code is properly formed with no typos. I'm baffled. Have I missed something really obvious?

3
  • Why are you using FALSE instead of false? Commented Apr 28, 2013 at 20:13
  • I think you forgot a return. You might also need to pass in your $i parameter by reference. Commented Apr 28, 2013 at 20:15
  • @Dai: While that question is pretty irrelevant and I have my doubts you're actually interested, the answer is that I use the uppercase version for readability reasons. Commented Apr 28, 2013 at 20:34

3 Answers 3

2

My guess is that you forgot a return:

function unserialize_recursive($data, $i = 0) {

$unserialized = unserialize($data);

if ($unserialized) {
    $i++;
}

if (!is_string($unserialized) || unserialize($unserialized) === FALSE) {
    /* placeholder - see explanation below */
    return array($i, $unserialized);
} elseif (unserialize($unserialized) !== FALSE) {
    return unserialize_recursive($unserialized, $i);
}
return FALSE;
}
Sign up to request clarification or add additional context in comments.

Comments

1

You use recursion in your function.

So your var_dump($unserialized) is called from a recursed invocation, but main invocation returns false.

You probably need to change "unserialize_recursive($unserialized, $i);" to

return unserialize_recursive($unserialized, $i);

Comments

0

You are missing return in front of

unserialize_recursive($unserialized, $i);

So it should be like this

return unserialize_recursive($unserialized, $i);

Without return function would run itself but then leave if condition and execute return FALSE. By placing return in front you end the current function and start another one.

1 Comment

Thanks - the line about ending the function and starting another one is the part that made most sense to me, though I'm actually still confused about why it doesn't work the way I first wrote it.

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.