TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   what is php serialization (http://www.talkphp.com/absolute-beginners/4737-what-php-serialization.html)

planepixel 07-15-2009 12:11 AM

what is php serialization
 
Hi
i came across php serialization but could not figure out what it is?i read php manual but did not get the point.

if somebody could throw some light about why and when we need to do serialization and what would happen if we do not do it?



thanks

Wildhoney 07-15-2009 01:48 AM

It's basically the representation of an array as a string. Good for storing the contents of an array in a file, and then being able to rebuild it with ease.

Take the following as a good example. Here we're saving the array, and then reloading it. It will give us the array just how we had it when we created it initially.

php Code:
function TalkPHP_Array_Save($aMyArray)
{
    $szSerialize = serialize($aMyArray);
    file_put_contents('myArray.txt', $szSerialize);
}

function TalkPHP_Array_Load()
{
    $szSerialize = file_get_contents('myArray.txt');
    return unserialize($szSerialize);
}

$aFruit = array('Orange', 'Apple', 'Banana');

/* Save and unset the array. */
TalkPHP_Array_Save($aFruit);
unset($aFruit);

/* Reload the array from a serialized string. */
$aFruit = TalkPHP_Array_Load();
print_r($aFruit);

cecilia 07-15-2009 04:54 PM

Isnt serialization used for converting an object into a string, allowing it to be stored on a database or passed by POST or GET?

At the moment, ive been trying to get a better understanding on when it is actually efficient to use serialization versus using delimiters+explode.

I didnt knew you can do that with an array too. I guess its nice for multidimensional ones. I just tested it, I guess it doesnt matter even if youre using what serialize uses as a delimiter on the array values. Darn, thats nice...

adamdecaf 07-15-2009 09:11 PM

Quote:

Originally Posted by cecilia (Post 27038)
Isnt serialization used for converting an object into a string, allowing it to be stored on a database or passed by POST or GET?

At the moment, ive been trying to get a better understanding on when it is actually efficient to use serialization versus using delimiters+explode.

I didnt knew you can do that with an array too. I guess its nice for multidimensional ones. I just tested it, I guess it doesnt matter even if youre using what serialize uses as a delimiter on the array values. Darn, thats nice...

Thats sterilization.


All times are GMT. The time now is 09:39 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0