Beyond JSON: Understanding PHP's Native String Serialization
Jul 25, 2025 pm 05:58 PMPHP's native serialization is more suitable for PHP's internal data storage and transmission than JSON, 1. Because it can retain complete data types (such as int, float, bool, etc.); 2. Support private and protected object properties; 3. Can handle recursive references safely; 4. There is no need for manual type conversion when deserializing; 5. It is usually better than JSON in performance; but it should not be used in cross-language scenarios, and unserialize() should never be called for untrusted inputs to avoid triggering remote code execution attacks. It is recommended to use it when it is limited to PHP environment and requires high-fidelity data.
When working with data storage or transmission in PHP, developers often reach for JSON as the default format. It's human-readable, widely supported, and language-agnostic. But PHP has its own native serialization mechanism that's been around longer and offers unique advantages — especially when dealing with complex PHP-specific data types. Let's look beyond JSON and explore PHP's native string serialization.

What Is PHP's Native Serialization?
PHP's serialize()
and unserialize()
functions convert PHP variables — including arrays, objects, resources (with limitations), and even closings (with workarounds) — into a storage string format. Unlike JSON, which is limited to basic types (strings, numbers, arrays, objects), PHP serialization preserves type information and object structure.
For example:

$data = [ 'name' => 'Alice', 'age' => 30, 'tags' => ['developer', 'php'], 'active' => true, 'balance' => 99.99 ]; $serialized = serialize($data); echo $serialized;
Output:
a:4:{s:4:"name";s:5:"Alice";s:3:"age";i:30;s:4:"tags";a:2:{i:0;s:9:"developer";i:1;s:3:"php";}s:6:"active";b:1;s:7:"balance";d:99.99;}
This string encodes not just structure, but types — strings ( s
), integers ( i
), booleans ( b
), doubles ( d
), and arrays ( a
). JSON would lose some of this fidelity, especially with floats vs ints or complex nested structures.

Key Advantages Over JSON
- Preserves PHP Types : JSON treats everything as a number, string, boolean, etc., without distinguishing between int and float. PHP serialization does.
- Supports Private and Protected Object Properties : When serializing objects, PHP keeps visibility information intact.
- Handles Recursion Safely : If an array or object references itself,
serialize()
handles it gracefully (marks it as recursive), whilejson_encode()
would fail. - No Manual Type Casting on Decode : With
json_decode()
, you often need to manually cast values back because everything comes back as string or float. Native unserialization restores original types automatically.
Example of recursion:
$arr = [1, 2]; $arr[] = &$arr; // self-reference echo serialize($arr); // Output: a:3:{i:0;i:1;i:1;i:1;i:2;i:2;r:2;}
The r:2
means "reference to variable at position 2" — something JSON can't represent.
How It Works Under the Hood
The serialized string uses a compact format where each value is prefixed with:
- A type identifier (
a
= array,s
= string,i
= int,b
= bool,d
= double,O
= object, etc.) - Length/type metadata
- The actual value
Structure breakdown:
a:2:{s:3:"foo";s:3:"bar";s:3:"baz";s:5:"quux";}
→ An array of 2 elements:
"foo" => "bar"
"baz" => "quux"
This format is not human-friendly , but it's precise and efficient for PHP-to-PHP communication.
When to Use Native Serialization (and When Not To)
Use PHP serialization when:
- Storing data in a PHP-only environment (eg, session storage, cache backends like APCu or Redis used internally)
- You need to preserve object state, private properties, or exact type fidelity
- Working with recursive data structures
- Performance matters —
serialize()
is often faster thanjson_encode()
/json_decode()
for complex PHP-native data
Avoid it when:
- Sharing data with other languages or APIs — JSON is standard and interoperable
- Security is a concern —
unserialize()
can be dangerous if used on untrusted input (leads to object injection attacks) - You need readingability or debugging ease
?? Never use
unserialize()
on user input. Malicious payloads can instantiate arbitrary objects and trigger destructors, leading to RCE (remote code execution) in vulnerable settings.
Alternatives and Best Practices
If you need more control or security, consider:
- JSON – for interoperability and safety
- igbinary – a binary serializer (alternative to
serialize
) that's more compact and faster, but still PHP-only - MessagePack – a fast, compact binary format with cross-language support
- Custom DTOs with explicit encoding/decoding – for critical systems where prediction matters
For safe storage, always validate and sanitize before serialization, and never trust unserialized data from external sources.
Basically, PHP's native serialization is powerful and underappreciated — especially when you're deep in the PHP ecosystem. While JSON wins for portability, PHP's serialize()
gives you richer type fidelity and handles edge cases that JSON can't. Just remember: great power comes with great responsibility, especially around unserialize()
.
The above is the detailed content of Beyond JSON: Understanding PHP's Native String Serialization. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Nullbytes(\0)cancauseunexpectedbehaviorinPHPwheninterfacingwithCextensionsorsystemcallsbecauseCtreats\0asastringterminator,eventhoughPHPstringsarebinary-safeandpreservefulllength.2.Infileoperations,filenamescontainingnullbyteslike"config.txt\0.p

sprintf and vsprintf provide advanced string formatting functions in PHP. The answers are: 1. The floating point accuracy and %d can be controlled through %.2f, and the integer type can be ensured with d, and zero padding can be achieved with d; 2. The variable position can be fixed using positional placeholders such as %1$s and %2$d, which is convenient for internationalization; 3. The left alignment and ] right alignment can be achieved through %-10s, which is suitable for table or log output; 4. vsprintf supports array parameters to facilitate dynamic generation of SQL or message templates; 5. Although there is no original name placeholder, {name} syntax can be simulated through regular callback functions, or the associative array can be used in combination with extract(); 6. Substr_co

TodefendagainstXSSandinjectioninPHP:1.Alwaysescapeoutputusinghtmlspecialchars()forHTML,json_encode()forJavaScript,andurlencode()forURLs,dependingoncontext.2.Validateandsanitizeinputearlyusingfilter_var()withappropriatefilters,applywhitelistvalidation

PHP's PCRE function supports advanced regular functions, 1. Use capture group() and non-capture group (?:) to separate matching content and improve performance; 2. Use positive/negative preemptive assertions (?=) and (?!)) and post-issue assertions (???)) and post-issue assertions (??

UTF-8 processing needs to be managed manually in PHP, because PHP does not support Unicode by default; 1. Use the mbstring extension to provide multi-byte security functions such as mb_strlen, mb_substr and explicitly specify UTF-8 encoding; 2. Ensure that database connection uses utf8mb4 character set; 3. Declare UTF-8 through HTTP headers and HTML meta tags; 4. Verify and convert encoding during file reading and writing; 5. Ensure that the data is UTF-8 before JSON processing; 6. Use mb_detect_encoding and iconv for encoding detection and conversion; 7. Preventing data corruption is better than post-repair, and UTF-8 must be used at all levels to avoid garbled code problems.

Rawstringsindomain-drivenapplicationsshouldbereplacedwithvalueobjectstopreventbugsandimprovetypesafety;1.Usingrawstringsleadstoprimitiveobsession,whereinterchangeablestringtypescancausesubtlebugslikeargumentswapping;2.ValueobjectssuchasEmailAddressen

PHP's native serialization is more suitable for PHP's internal data storage and transmission than JSON, 1. Because it can retain complete data types (such as int, float, bool, etc.); 2. Support private and protected object properties; 3. Can handle recursive references safely; 4. There is no need for manual type conversion during deserialization; 5. It is usually better than JSON in performance; but it should not be used in cross-language scenarios, and unserialize() should never be called for untrusted inputs to avoid triggering remote code execution attacks. It is recommended to use it when it is limited to PHP environment and requires high-fidelity data.

Processlargefilesline-by-lineorinchunksusingfgets()orfread()insteadofloadingentirefilesintomemorywithfile()orfile_get_contents().2.Minimizeunnecessarystringcopiesbyavoidingchainedstringfunctions,breakingdownoperations,andusingunset()onlargestringswhe
