Signature
var_export(mixed $value, bool $return = false): ?stringPurpose
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 tofalse. Whenfalse, the function prints the representation and returnsnull. Whentrue, 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 isstdClass, 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.
$data = ['id' => 7, 'active' => true, 'score' => 9.5, 'note' => null];
var_export($data);
echo "\n";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.
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";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.
enum Suit
{
case Hearts;
case Spades;
}
echo var_export(Suit::Hearts, true), "\n";\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_precisionsetting, 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
stdClassobjects 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
- Forgetting
true. Without it the function echoes and returnsnull, so concatenating its result into a string loses the content. - Treating it as a storage format for objects. For arbitrary objects,
serialize()or JSON is usually more dependable. - Expecting human-friendly output. Use
print_r()for a compact readable dump, orvar_dump()when you need sizes and types. - 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.
- Trying to export circular structures or resources. These are not supported, so restructure the data first.
Related functions
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
$data = ['id' => 7, 'active' => true, 'score' => 9.5, 'note' => null];
var_export($data);
echo "\n";
array ( 'id' => 7, 'active' => true, 'score' => 9.5, 'note' => NULL, )
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";
1.0 'It\'s' false NULL
enum Suit
{
case Hearts;
case Spades;
}
echo var_export(Suit::Hearts, true), "\n";
\Suit::Hearts