var_export()

var_export() — Outputs or returns a parsable string representation of a variable.

Built-in function of PHP 8.5. The manual lists it as available in PHP 4 >= 4.2.0, PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.

What it does

var_export() gets structured information about the given variable. It is similar to var_dump() with one exception: the returned representation is valid PHP code.

Signature

var_export(mixed $value, bool $return = false): ?string

Parameters

ParameterTypeWhat it takes
$valuemixedThe variable you want to export.
$returnboolIf used and set to true, var_export() will return the variable representation instead of outputting it.

Return value

Returns the variable representation when the return parameter is used and evaluates to true. Otherwise, this function will return null.

Example

A runnable snippet:

$a = array (1, 2, array ("a", "b", "c")); var_export($a);

Output:

array ( 0 => 1, 1 => 2, 2 => array ( 0 => 'a', 1 => 'b', 2 => 'c', ), )

Changelog

VersionChange
8.2.0Exported class names are now fully qualified; previously, the leading backslash was omitted.
7.3.0Now exports stdClass objects as an array cast to an object ((object) array( ... )), rather than using the nonexistent method stdClass::__setState(). The practical effect is that now stdClass is exportable, and the resulting code will even work on earlier versions of PHP.

See also

print_r(), serialize(), var_dump()

All PHP 8.5 built-in functions

Reference card built from the PHP manual page var_export() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.