But if the data were fetched from the database and the script tried to unserialize() them the following error occured:
So why is this?
Lets say you have an array with 3 indizes containing 2 strings and one integer.
As you can see the single quote in the second array-element is escaped by a backslash.
If you now serialize the data you get the following string
If this string is now written to the database the backslashes disappears and the field contains the following data:
As the considerate reader might have discovered the length of the highlighted string was first 25 characters and is after inserting into the database 24 characters long.
So if PHP tries to unserialize the string it thinks it has to read 25 characters but there are only 24. This throws the above error.
So how can you circumvent this?
So the backslashes are removed before serializing the data and added afterwards to prevent database malfunctions or errors.