Check if string is serialized in PHP

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!


  • $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.