views:

109

answers:

1

Hi everyone,

I am in the middle of building a cache layer for the Redis DB to my application and I have come to the point where's it's about to take care of arrays.

I wonder if there's any good (high performance!) way of controlling an string to be serialized or not with PHP?

Thanks a lot!

+5  A: 
$array = @unserialize($string);
if ($array === false && $string !== 'b:0;') {
    // woops, that didn't appear to be anything serialized
}

The $string !== 'b:0;' checks to see if the serialized string may have been the value false. If this check is important to you you may want to trim the serialized string or otherwise preprocess it to make sure this works.

deceze
you don't have to take the risk, you can check error_get_last
Artefacto
@Artefacto This may or may not be useful, as it can be hard to tell whether the last error was really thrown here or sometime earlier. A better way may be to look at the serialized string to see if it looks like a single serialized `false`. Anyway, who serializes `false` values? ;)
deceze
Hi Deceze! Sounds great, I will definitely try that out! Have a great weekend!
Industrial