In PHP, the serialize() function is used to convert a PHP variable into a stored or transferable string. This is usually used to store data to a database or transfer it over a network. However, many people find that the returned string is not intuitive and difficult to understand or debug after using the serialize() function.
The string returned by serialize() is encoded from the original structure of a PHP object or array. This string is not a human-friendly format, but is designed for machines to parse and restore to original data structures. So the result looks like a long list of meaningless characters.
For example, for a simple array:
$array = ['name' => 'Alice', 'age' => 25];
$serialized = serialize($array);
echo $serialized;
The output might be a string like this:
a:2:{s:4:"name";s:5:"Alice";s:3:"age";i:25;}
Although PHP can be parsed, it takes some extra effort for developers to understand it. It contains details of data types and key-value pairs, but has no direct readability.
Let's take a look at the components of this output string in detail:
a:2 means an array with two elements inside.
s:4:"name" represents a string with a length of 4 and a value of "name" .
s:5:"Alice" represents a string with a length of 5 and a value of "Alice" .
s:3:"age" represents a string with a length of 3 and a value of "age" .
i:25 represents an integer with a value of 25.
Although we know this information, it still seems very lengthy and unintuitive.
To render data in a more readable format, PHP provides the var_export() function, which returns a formatted PHP code string, showing the structure of the variable. This output is easier to understand than serialize() . For example:
$array = ['name' => 'Alice', 'age' => 25];
echo var_export($array, true);
The output is as follows:
array (
'name' => 'Alice',
'age' => 25,
)
This format clearly shows the key-value pairs of the array, making it easier to understand.
If you want serialized data to be more cross-language compatibility and easier to debug and view, you can use the json_encode() function to convert the data to JSON format. The JSON format is not only human-friendly, but is also widely used in data exchange in modern applications.
$array = ['name' => 'Alice', 'age' => 25];
echo json_encode($array, JSON_PRETTY_PRINT);
The output is as follows:
{
"name": "Alice",
"age": 25
}
In this way, data is not only easy to read, but also easily transmitted between different programming languages or systems.
If you want more complex serialization functions, or want to customize the serialized data format, you can consider using a third-party library. For example, Symfony VarDumper provides a more powerful data presentation tool that generates more readable debug information.
use Symfony\Component\VarDumper\VarDumper;
$array = ['name' => 'Alice', 'age' => 25];
VarDumper::dump($array);
This outputs a formatted, easy to read structured representation.
Although PHP's serialize() function can convert data into a format that can be stored or transferred, the generated strings are usually unfriendly to humans. To solve this problem, you can use the following methods:
Use var_export() to output a more intuitive PHP code format.
Use json_encode() to output the JSON format, which is easy to read and widely compatible with humans.
Use third-party libraries such as Symfony VarDumper to generate more friendly debug output.
Through these methods, we can ensure that the data display method is more in line with the needs of developers and improve the efficiency of debugging and data processing.