Converting a value to a sequence of bits to be able to store the value in a memory buffer or a file or to transmit through the network is called serialization of data and object serialization in PHP is done by making use of a function called serialize() function which converts a value to a storable representation or serializes the given value and the value to be serialized is passed as a parameter to the serialize function and a string as a sequence of bytes which represents the given value to be serialized is returned by serialize() function and this returned string can be stored anywhere.
ADVERTISEMENT Popular Course in this category PHP DEVELOPER - Specialization | 8 Course Series | 3 Mock TestsStart Your Free Software Development Course
Web development, programming languages, Software testing & others
The syntax to declare serialize() function in PHP is as follows:
serialize(value);
where value is the value to be serialized as a sequence of bytes to be stored anywhere.
Different examples are mentioned below:
PHP program to illustrate object serialization to convert a given value as a sequence of bits so that it can be stored anywhere:
Code:
<html> <body> <?php #The array of data to be serialized is passed to the serialize function and the returned string is stored in a variable called value $value = serialize(array("Welcome", "to", "PHP")); #The returned string from the serialize() function is displayed as the output on the screen echo "The data after serialization using serialize() function is as follows:\n"; echo $value; ?> </body> </html>
Output:
In the above program, the array of data to be serialized is passed to the serialize function and the returned string is stored in a variable called value. Then the returned string from the serialize() function is displayed as the output on the screen.
PHP program to illustrate object serialization to convert a given value as a sequence of bits so that it can be stored anywhere:
Code:
<html> <body> <?php #The array of data to be serialized is passed to the serialize function and the returned string is stored in a variable called value $value = serialize(array("Learning", "is", "fun")); #The returned string from the serialize() function is displayed as the output on the screen echo "The data after serialization using serialize() function is as follows:\n"; echo $value; ?> </body> </html>
Output:
In the above program, the array of data to be serialized is passed to the serialize function and the returned string is stored in a variable called value. Then the returned string from the serialize() function is displayed as the output on the screen.
PHP program to illustrate object serialization to convert a given value as a sequence of bits so that it can be stored anywhere:
Code:
<html> <body> <?php #The array of data to be serialized is passed to the serialize function and the returned string is stored in a variable called value $value = serialize(array("EDUCBA", "is", "informative")); #The returned string from the serialize() function is displayed as the output on the screen echo "The data after serialization using serialize() function is as follows:\n"; echo $value; ?> </body> </html>
Output:
In the above program, the array of data to be serialized is passed to the serialize function and the returned string is stored in a variable called value. Then the returned string from the serialize() function is displayed as the output on the screen.
PHP program to illustrate object serialization to convert a given value as a sequence of bits so that it can be stored anywhere:
Code:
<html> <body> <?php #The array of data to be serialized is passed to the serialize function and the returned string is stored in a variable called value $value = serialize(array("India", "is", "beautiful")); #The returned string from the serialize() function is displayed as the output on the screen echo "The data after serialization using serialize() function is as follows:\n"; echo $value; ?> </body> </html>
Output:
In the above program, the array of data to be serialized is passed to the serialize function and the returned string is stored in a variable called value. Then the returned string from the serialize() function is displayed as the output on the screen.
PHP program to illustrate object serialization to convert a given value as a sequence of bits so that it can be stored anywhere:
Code:
<html> <body> <?php #The array of data to be serialized is passed to the serialize function and the returned string is stored in a variable called value $value = serialize(array("We", "love", "India")); #The returned string from the serialize() function is displayed as the output on the screen echo "The data after serialization using serialize() function is as follows:\n"; echo $value; ?> </body> </html>
Output:
In the above program, the array of data to be serialized is passed to the serialize function and the returned string is stored in a variable called value. Then the returned string from the serialize() function is displayed as the output on the screen.
In this article, we have learned the concept of object serialization in PHP through definition, syntax, and working of serialize() function in PHP through programming examples and their outputs.
The above is the detailed content of PHP Object Serialization. For more information, please follow other related articles on the PHP Chinese website!