When you do that, you lose the original data types. Say the input box required a number – now you receive it as a string. If you have a date field, then that’s even more work to convert back.
Serialization handles types and so you can easily restore everything back. I assume you’ve used PHP sessions before – it stores those to disk usually. Wouldn’t it be annoying if you manually had to convert numbers back to numbers and arrays back to arrays every time?
Why would you have to convert data to binary to store it in a file?
And why would data need to be converted into binary to transmit it?
When you submit an HTML form using GET, you send data as a long text string over the Internet, right? (And you don’t need to convert it to binary first?!)
Such strings can be stored in a database, session etc.
Why can’t unserialized data be stored in a database, session, etc?
Your example: it shows an application that has stored a serialized version of a shopping cart in the session (and then retrieved it and unserialized it). Which is kinda silly, as you can store unserialized data in $_SESSION as well.
Sending it with a “decoder ring” like in my breakfast cereal?!
Who’s transmitting stuff?
The example said, “And when sending data over the network…”
Your browser performs this transformation for you, it serialises the form data and sends it to the server in the HTTP request. PHP then unserializes this and makes it available in the GET and POST superglobals.
So when you submit data in an HTML form to the PHP server, you don’t need to “serialize”/“unserialize” anything manually?
You’re not converting it to binary, you’re serializing it. Essentially, transforming it into a transportable structure. In this case, a simple string.
Who’s transmitting stuff?
Your browser performs this transformation for you, it serialises the form data and sends it to the server in the HTTP request. PHP then unserializes this and makes it available in the GET and POST superglobals.
You don’t really need to be able to read this, but know that it can be stored.
unserializing that string would give us:
array (
'key1' => 'value1',
'key2' => 'value2',
)
Your example: it shows an application that has stored a serialized version of a shopping cart in the session (and then retrieved it and unserialized it). Which is kinda silly, as you can store unserialized data in $_SESSION as well.
In computer science, in the context of data storage and transmission, serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be “resurrected” later in the same or another computer environment