Serialization – PHP

Have you ever needed to store a complex variable in a database or a text file? You do not have to come up with a fancy solution to convert your arrays or objects into formatted strings, as PHP already has functions for this purpose.

There are two popular methods of serializing variables. Here is an example that uses the serialize() andunserialize():

This was the native PHP serialization method. However, since JSON has become so popular in recent years, they decided to add support for it in PHP 5.2. Now you can use the json_encode() and json_decode()functions as well:

It is more compact, and best of all, compatible with javascript and many other languages. However, for complex objects, some information may be lost.

Deja un comentario

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *