Updated April 4, 2023
Introduction to PHP Object Serialization
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.
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.
Working of serialize() function in PHP
- 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 the serialization of data.
- 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.
- The value to be serialized is passed as a parameter to the serialize function.
- A string as a sequence of bytes which represents the given value to be serialized is returned by the serialize() function and this returned string can be stored anywhere.
Examples of PHP object serialization
Different examples are mentioned below:
Example #1
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.
Example #2
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.
Example #3
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.
Example #4
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.
Example #5
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.
Conclusion
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.
Recommended Articles
This is a guide to PHP Object Serialization. Here we discuss the concept of object serialization in PHP through definition, syntax, and working of serialize() function. You may also have a look at the following articles to learn more –