var_export

Outputs or returns a variable as valid, parsable PHP code.

Signature
var_export(mixed $value, bool $return = false): ?string
  • Added in PHP 4.2.0

Parameters

ParameterTypeDescription
$valuemixedThe variable to export.
$returnoptionalbool= falseWhen true, return the representation as a string instead of printing it.

Return value

?stringThe variable representation as a string when $return is true; otherwise null, and the representation is printed.

Errors

Emits a warning and exports NULL when the value contains a circular reference. Resources always export as NULL.

Changelog

  • PHP 8.2.0Exported class names are now fully qualified with a leading backslash; previously the backslash was omitted.
  • PHP 7.3.0stdClass objects are exported as (object) array( ... ) instead of the nonexistent stdClass::__set_state().

Its one job: round-trippable code

Unlike var_dump() and print_r(), the output of var_export() is syntactically valid PHP. Strings come back quoted and escaped, null prints as NULL, booleans as true/false, and arrays as array ( 0 => ... ). That makes it the right tool whenever the dump needs to become code again.

When to reach for it

Generating cached config files is the canonical use: build the array once, write "<?php return " . var_export($cfg, true) . ";" to disk, then require it. OPcache keeps the compiled result in memory, so reads cost essentially nothing — this is how framework config and route caches work. It is also the cleanest way to paste a real value into a unit test as a fixture.

What trips people up

Objects only round-trip if their class implements the static __set_state() magic method; stdClass is the exception, exported as an (object) array(...) cast since PHP 7.3. Resources always export as NULL — silently. Circular references cause a warning rather than a usable result; use serialize() for those. Floats use the serialize_precision setting, so var_export(1.1) may print 1.1000000000000001, which is correct for round-tripping but startling in a config file.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Write a compiled configuration or routing array to a PHP file that OPcache can keep in memory.
  • Copy a real runtime value into a test as a literal fixture array.
  • Compare two nested arrays in a diff by exporting both to strings.

Frequently asked questions

What is the difference between var_export and var_dump?

var_export() produces valid PHP code you can eval() or write to a file, while var_dump() produces a human-readable listing with types and sizes that is not parsable. Use var_export() when the output has to become code again, var_dump() when you are debugging.

How do I get var_export output as a string?

Pass true as the second argument: $code = var_export($value, true). With the default false it prints directly and returns null.

Why does var_export show NULL for my object or file handle?

Resources cannot be exported at all and always become NULL. Objects export as a __set_state() call, which only evaluates back if their class defines that static method — stdClass is the exception and exports as an (object) array cast.

More examples

How it differs from var_dump and print_r

print_r() renders null and false as empty space, so its output cannot be parsed back. var_export()'s output feeds straight into eval() and produces an identical array — that round trip is the whole point of the function.

Example · php
<?php
$value = [1, 'two', 3.5, true, null];

echo "var_export:\n";
var_export($value);

echo "\n\nprint_r:\n";
print_r($value);

echo "\nRe-evaluated:\n";
$copy = eval('return ' . var_export($value, true) . ';');
var_dump($copy === $value);

Objects need __set_state()

The exported code is a call to \Point::__set_state() with the property array — fully qualified with a leading backslash since PHP 8.2. Without that static method defined, the export still prints but evaluating it fatals with an undefined-method error.

Example · php
<?php
class Point {
    public function __construct(public int $x = 0, public int $y = 0) {}
    public static function __set_state(array $p): self {
        return new self($p['x'], $p['y']);
    }
}

$code = var_export(new Point(3, 4), true);
echo $code, "\n";

$clone = eval("return {$code};");
var_dump($clone == new Point(3, 4));

Resources and floats: the silent surprises

The stream becomes NULL in the export with no warning. The float shows serialize_precision output (17 digits) rather than var_dump()'s display precision, which is why exported config values sometimes look like 1.1000000000000001.

Example · php
<?php
$handle = fopen('php://memory', 'r+');
$data = ['stream' => $handle, 'ratio' => 1.1];

var_export($data);
echo "\n";
fclose($handle);

var_export(0.1 + 0.2);
echo "\n";
var_dump(0.1 + 0.2);

Discussion

  • Be the first to comment on this lesson.