Asked 7 years ago
28 Dec 2016
Views 1033
python

python posted

what is use of serialize function in php

what is use of serialize function in php ?
Mitul Dabhi

Mitul Dabhi
answered Nov 30 '-1 00:00

serialize will convert object , array to single string with its type so it easy to store . transport and easy to use

in other word , we can say its return metadata and data for passed value

 echo serialize(array("php","development",12));

will return
a:3:{i:0;s:3:"php";i:1;s:11:"development";i:2;i:12;} , now this string easy to store any varchar typed field at table at database.


 class serialize{
   var $serialize = array("php","easy","learn");
}
$serialize =new serialize();
echo serialize($serialize);



will return
O:9:"serialize":1:{s:9:"serialize";a:3:{i:0;s:3:"php";i:1;s:4:"easy";i:2;s:5:"learn";}}

if you want to return to his original form use unserialize


  print_r(unserialize(serialize(array("php","development",12))));


will give you array as we define .



ravi

ravi
answered Nov 30 '-1 00:00

some situation where i used serialize in php

1.Data passing to one page to other page
without relying on a session variable,pass data one page to another page , suppose multiple page of signup form. use serialize() to serialize object or data to , pass it on to the next page as a hidden form field, then unserialize() it.

2. to store cache . in files or database.
to store cache in files , you can use serialize()

3.to save session to database.
it can be used to store temprorary data to database. so make it compact by serialize() before saving database.

4.to send data over network by api.
suppose we want to get talked between two server where data sent after serialize by php script and other script in php recieve data and unserialize() it.
Post Answer