var_export()
The var_export() function is a built-in function in PHP that outputs a string representation of a variable or expression that can be used as PHP code. It can be
Introduction
var_export() is a built-in PHP function that produces a valid PHP code representation of a variable. Unlike a plain print, the string it returns is parseable: you could copy it back into a .php file (or pass it to eval()) and get an identical value. That makes it ideal for:
- Generating cached config or data files — write the export to disk and
requireit later. - Inspecting a variable's exact structure while debugging.
- Snapshotting expected values in tests and documentation.
This page covers the syntax, the $return flag, how each data type is rendered, and how var_export() differs from var_dump() and print_r().
Syntax
var_export(mixed $value, bool $return = false): ?string| Parameter | Description |
|---|---|
$value | The variable or expression to export. |
$return | When false (default), the representation is printed directly and the function returns null. When true, the representation is returned as a string instead of being printed. |
The most common mistake is forgetting the second argument: $result = var_export($x); leaves $result as null because the output went straight to stdout. To capture it, pass true.
Basic example
Here $var1 is an integer, $var2 a string, and $array an indexed array. Each call returns a string that is itself valid PHP:
10
'Hello, world!'
array (
0 => 'apple',
1 => 'banana',
2 => 'cherry',
)Notice that strings are wrapped in single quotes and arrays keep their keys explicit — the output of an array could be assigned straight to a variable.
How each type is rendered
<?php
var_export(true); echo "\n"; // true
var_export(null); echo "\n"; // NULL
var_export(3.14); echo "\n"; // 3.14
var_export('a'); echo "\n"; // 'a'
$assoc = ['name' => 'Ann', 'age' => 30];
var_export($assoc); echo "\n";
?>Output:
true
NULL
3.14
'a'
array (
'name' => 'Ann',
'age' => 30,
)Booleans become true/false, null becomes the uppercase NULL, and associative arrays preserve their string keys — all syntactically valid PHP.
Exporting objects
For objects, var_export() emits a __set_state() call so the structure can, in principle, be reconstructed:
<?php
class Point {
public int $x = 1;
public int $y = 2;
}
var_export(new Point());
?>\Point::__set_state(array(
'x' => 1,
'y' => 2,
))To actually rebuild such an object from the export, the class must define a static __set_state() method. Without it, evaluating the output throws an error — so for round-tripping objects, serialize() is usually a better fit.
var_export() vs. var_dump() vs. print_r()
| Function | Output format | Shows types? | Returnable string? | Valid PHP? |
|---|---|---|---|---|
var_export() | Parseable PHP code | Implicitly | Yes ($return = true) | Yes |
var_dump() | Type + value + length | Yes (explicit) | No (prints only) | No |
print_r() | Human-readable tree | No | Yes (2nd arg) | No |
Use var_export() when you need code you can store or re-run, var_dump() when you need types and string lengths for debugging, and print_r() for a quick readable glance.
A practical use: cached data files
Because the export is valid PHP, you can persist computed data and load it fast on the next request:
<?php
$config = ['debug' => true, 'level' => 3, 'tags' => ['a', 'b']];
// Write a loadable PHP file.
file_put_contents('cache.php', '<?php return ' . var_export($config, true) . ';');
// Later, somewhere else:
$loaded = require 'cache.php';
?>$loaded is identical to the original $config array — no parsing of JSON or unserialize step required.
Conclusion
var_export() turns a variable into ready-to-run PHP code. Pass $return = true to capture that code as a string, remember that null prints as NULL and strings are single-quoted, and reach for serialize() instead when you need to round-trip objects. For type-aware debugging, compare it with var_dump() and print_r().