Convert object to string in PHP

I want to convert an object to a string in PHP. Specifically, I am trying to execute a response to a mysql query, and I am trying to convert it to something that I can write to a file and use later.

Of course, when you try to write an object to a file, PHP screams correctly: Cheated fatal error: an object of class DB_result cannot be converted to a string in .....

Alternatively, if there is another way to write the result of the mysql query to a file, this also works. I play with a home caching project :)

+3
source share
2 answers

Is serialization possible? It will take an object / array and convert it to a string (which then may not be serialized later)

+8
source

json_encode and json_decode will also fulfill many of the properties you are looking for through serialization. The advantage is that you can send JSON-encoded data to a web browser, and JavaScript can view and modify properties, such as its own JavaScript object. In addition, JSON is lighter than serialized data because its syntax is much more compact.

+3
source

All Articles