So I need to encode an array in PHP and store it in plain text in MySQL database, my question is should I use serialize() or json_encode()? What are the advantages and disadvantages of each of them?
I think either of them would do in this situation. But which one would you prefer and why? If it is for something other than an array?
Main advantage of
serialize: it’s specific to PHP, which means it can represent PHP types, including instances of your own classes — and you’ll get your objects back, still instances of your classes, when unserializing your data.Main advantage of
json_encode: JSON is not specific to PHP : there are libraries to read/write it in several languages — which means it’s better if you want something that can be manipulated with another language than PHP.A JSON string is also easier to read/write/modify by hand than a serialized one.
On the other hand, as JSON is not specific to PHP, it’s not aware of the stuff that’s specific to PHP — like data-types.
As a couple of sidenotes :
whereclauses, nor update it without the intervention of PHP…