You may have noticed the strange strings in WordPress’ wp_options table(in some option_value) like this:
a:3:{i:0;i:123;s:3:"abc";i:456;i:789;s:3:"def";}
It seems you can decipher it but it is not very easy to read. What is it? Well, it is a serialized php variable. WordPress themes/plugins like to store their options (in a php variable) into database as a serialized string, and restore the variable by reading the string from the database then unserializing it. You can serialize a php object. You can serialize/unserialize a php array as well as follows.
$a=array(123,"abc"=>456, 789=>"def"); $b=serialize($a); $a=unserialize($b);
The serialized string is deciphered as follows:
array:the array has 3 members:{the key of the first member is an integer:the key is 0;the value of the first member is an integer:the value is 124;the key of the second member is a string:the string has 3 characters:the string key is “abc”;the value of the second member is an integer:the value is 123;the key of the third member is an integer:the key is 789;the value of the third member is a string:the string has 3 characters:the string is “def”;}
The following example serializes/unserializes a php object:
class A
{
var $a;
var $b;
}
$a=new A;
$a->a=123;
$a->b="abc";
$b=serialize($a);
$a=unserialize($b);
The serialized string for the object is:
O:1:"A":2:{s:1:"a";i:123;s:1:"b";s:3:"abc";}
You now should be able to decipher most of the fields. Unlike for an array, O stands for Object, O:1:”A”:2 is deciphered as Object:the name of the class of the object has 1 character:the name of the class of the object is “A”:the object has 2 members.
If you are familiar with other programming languages, you may notice the serialized string is similar to a Json string. But it is actually not a json string(it contains more information than the JSON string for the same variable). json_encode is also a convenient way to convert a php variable to a string for storing (to database or file) and transferring. On the contrary, json_decode is used to read from a string and reconstruct the php variable.
Here is an example to encode an array to a JSON string and decode the JSON string to a php variable:
$a=array(123,"abc"=>456, 789=>"def"); $b=json_encode($a); $c=json_decode($b);
The JSON string is:
{"0":123,"abc":456,"789":"def"}
Note that, the php associative array is encoded to a JSON string that represents an object. Although an ordinary php array can be encoded as array or object by setting or not setting the JSON_FORCE_OBJECT parameter of json_encode, a php associative array is always encoded as an object. The difference between JSON encoding and serialization is that all keys of the php array are encoded as strings in JSON regardless they are integer keys or string keys, while the keys keep their types in serialized strings. Because json_encode always encodes associative arrays as objects, there also exists a difference between unserialization and json_decode: unserialize restores the original array while json_decode by default returns a stdClass class with the array keys being its members, although you can provide the second parameter of json_decode as true to decode the object JSON string to a php associative array.
object(stdClass)#1 (3) {
["0"]=>
int(123)
["abc"]=>
int(456)
["789"]=>
string(3) "def"
}
In the following example, we can see how to encode a php object to a JSON string and how to decode the JSON string to a stdClass object:
class A
{
var $a;
var $b;
}
$a=new A;
$a->a=123;
$a->b="abc";
$b=json_encode($a);
$c=json_decode($b);
The JSON string in the above example is:
{"a":123,"b":"abc"}
The decoded object is not an instance of class A but an instance of class stdClass, because the JSON string looses the type information about class A, even the class name “A”:
object(stdClass)#3 (2) {
["a"]=>
int(123)
["b"]=>
string(3) "abc"
}
Another difference between json_encode and serialize is that json_encode encodes a php variable into a printable string while the output of serialize is actually a binary string:
Note that this is a binary string which may include null bytes, and needs to be stored and handled as such. For example, serialize() output should generally be stored in a BLOB field in a database, rather than a CHAR or TEXT field.
This explains why you may encounter problems when editing the option_value of the wp_option table in phpMyAdmin(by double-clicking the cell), because the option_value is of longtext type while some binary option values generated through the serialize function may not be editable.
The other differences between serialize and json_encode can be found here. This article is also a good reference.