Store cache in files in PHP

var_export vs serialize vs json_encode

Antal Áron • March 7, 2017

Recently, I was in a situation, when I had to cache some data in PHP. It was not a big project, so I didn't wanted to use redis or any other heavy tool. I decided to store the data in file. Let's serialize! ... Or var_export! ... Or json_encode?! Hehh.

I had no time than, I chose var_export. But I decided to benchmark my choice.


Both serialize and json_encode have symmetric functions, which reproduce the original data. But var_export slightly differs. It creates a PHP-parsable string, it can be evaluated by eval, or can be included by include.

Here you can find the benchmark code.

And here is the result (PHP 7.0, Ubuntu 16.04, Intel i5-4590 @3.30GHz, SSD):

Grey is the import, Green is the export

For me a little bit surprising.


The serialize or the json_encode functions are the best choices. In cases, where import is applied more frequently, maybe the serialize is the best choice.