Re: pack vs. serialize

From: Date: Sun, 17 Jun 2007 23:01:40 +0000
Subject: Re: pack vs. serialize
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-257242@lists.php.net to get a copy of this message
Nathan Nobbe dedi ki: > all, > > i have recently stumbled upon the > > pack<http://www.php.net/manual/en/function.pack.php>method. > can this be used as a substitute for > serialize<~¸Û > ÒÝ�yòé:I‹¬æhttp://www.php.net/manual/en/function.serialize.php> > ? > if so, is there any rationalization for this; like would it perhaps > create a more > compact representation of the data or run more quickly? > it looks like pack cannot be used on object as i didnt see object in the > format listing; > what would be a typical application of the pack method? I've not used serialize(), but I think it packs its input into some proprietary (internal) format best suited to handle as a single entity (pass it to-fro, store it in database, etc.). Ergo, a serialized entity cannot be used as it is - you need to unserialize it back before use. OTOH, pack() returns the internal binary representation of its input as a binary string. This may not always be the optimal way to handle a given collection of data as in the case for serialize(), but it enables you to access, use and manipulate internal representations of the data, be it just a single variable or an assorted structure. I once used pack to obtain a 128 bit long binary string (xor key for rudimentary file encryption) by packing four 32-bit integers into a binary string and then using it as the xor key. (FWIW, bitwise operations work both on integers and "strings" - binary or otherwise.) Kind regards, -- Abdullah Ramazanoglu aramazan ÄT myrealbox D0T cöm

« previous php.general (#257242) next »