Types7 min readExamples run on PHP 8.5.10

var_export()

Learn how PHP 8.4 var_export() turns values into parsable PHP code, how its return flag works, and which values it cannot export safely.

Signature

var_export(mixed $value, bool $return = false): ?string
Available since PHP 4.2.07.48.08.18.28.38.48.5All versions →

Purpose

var_export() converts a PHP value into a string of valid PHP code. It is close to var_dump() in spirit, but the goal is different. var_dump() is for inspecting a value while debugging. var_export() produces text you could paste into a source file to recreate the value. That makes it handy for generating configuration files, writing test fixtures, or showing exactly what a variable holds, including the difference between false, NULL, 1 and 1.0.

Signature

Parameters and defaults

  • $value: any value you want to export. Scalars, null, arrays, enums and most objects are accepted.
  • $return: defaults to false. When false, the function prints the representation and returns null. When true, it returns the representation as a string and prints nothing.

Return value and failure cases

The return type is ?string. You only get a string when $return is truthy. Otherwise the result is null, so writing $code = var_export($x); leaves $code empty while the text goes straight to output. That is a frequent surprise.

Some values cannot be turned into reliable code:

  • Resources cannot be exported in a useful way.
  • Circular references are not handled, because there is no finite piece of PHP code that describes them. Use serialize() if you need a full representation of such structures.
  • Objects are exported as \ClassName::__set_state(array(...)). To evaluate that code later, the class must define a static __set_state() method. The exception is stdClass, which is exported as an array cast to an object: (object) array(...).

Example 1: exporting an array

Keys and value types are preserved, strings are single quoted, and every element ends with a comma.

Example
<?php
$data = ['id' => 7, 'active' => true, 'score' => 9.5, 'note' => null];
var_export($data);
echo "\n";
Output
array (
  'id' => 7,
  'active' => true,
  'score' => 9.5,
  'note' => NULL,
)

The array is printed in long array ( form, and null appears as NULL. The output has no trailing newline, so the example adds one itself.

Example 2: returning a string for scalars

With $return set to true, you can build strings, store them, or compare them. Notice how the export keeps type information that echo would lose.

Example
<?php
echo var_export(1.0, true), "\n";
echo var_export("It's", true), "\n";
echo var_export(false, true), "\n";
echo var_export(null, true), "\n";
Output
1.0
'It\'s'
false
NULL

The float 1.0 keeps its decimal point so it still reads back as a float. A single quote inside a string is escaped with a backslash, and booleans and null are written as false and NULL.

Example 3: exporting an enum case

Since PHP 8.2 exported class names are fully qualified, which means a leading backslash. Enum cases are exported as a constant-style reference.

Example
<?php
enum Suit
{
    case Hearts;
    case Spades;
}

echo var_export(Suit::Hearts, true), "\n";
Output
\Suit::Hearts

The result is code that refers to the case directly, so no __set_state() method is involved.

Edge cases

  • Floats are exported with enough precision to read back exactly under the default serialize_precision setting, and whole floats keep a .0.
  • Strings containing special characters are escaped so the result is still a valid PHP literal.
  • Nested arrays are printed on several lines with indentation. The exact whitespace is part of the output, so never compare exported text by eye or with a hand-trimmed string.
  • Exported stdClass objects use the (object) array(...) form, which is valid code even in older PHP versions.
  • Objects of your own classes give code that only works when __set_state() exists and rebuilds the object from the supplied array.

Common mistakes

  1. Forgetting true. Without it the function echoes and returns null, so concatenating its result into a string loses the content.
  2. Treating it as a storage format for objects. For arbitrary objects, serialize() or JSON is usually more dependable.
  3. Expecting human-friendly output. Use print_r() for a compact readable dump, or var_dump() when you need sizes and types.
  4. Evaluating exported text from untrusted data. Generating code is safe only if you control what runs it later. Prefer including generated files from trusted build steps rather than evaluating strings at runtime.
  5. Trying to export circular structures or resources. These are not supported, so restructure the data first.
  • var_dump() prints types and sizes for debugging.
  • print_r() prints a readable summary and can also return a string.
  • serialize() creates a storable representation that handles object graphs and references.
  • json_encode() creates portable text for exchanging data with other systems.

Recap

var_export() gives you a parsable PHP representation of a value. Pass true as the second argument when you want a string. Remember that it returns null otherwise, that objects need __set_state() to be rebuilt (except stdClass), and that circular references and resources are outside its abilities.

Tested examples

Exporting an associative array
<?php
$data = ['id' => 7, 'active' => true, 'score' => 9.5, 'note' => null];
var_export($data);
echo "\n";
Output
array (
  'id' => 7,
  'active' => true,
  'score' => 9.5,
  'note' => NULL,
)
Returning exported scalars as strings
<?php
echo var_export(1.0, true), "\n";
echo var_export("It's", true), "\n";
echo var_export(false, true), "\n";
echo var_export(null, true), "\n";
Output
1.0
'It\'s'
false
NULL
Exporting an enum case
<?php
enum Suit
{
    case Hearts;
    case Spades;
}

echo var_export(Suit::Hearts, true), "\n";
Output
\Suit::Hearts