var_export

Outputs or returns a parsable string representation of a variable

Description

stringnull var_export(mixed $value, bool $return = false)

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.

Parameters

value

The variable you want to export.

return

If used and set to true, var_export will return the variable representation instead of outputting it.

Return Values

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

Changelog

Version Description
8.2.0 Exported class names are now fully qualified; previously, the leading backslash was ommitted.
7.3.0 Now 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.

Examples

Example #1 var_export Examples

<?php
$a = array (1, 2, array ("a", "b", "c"));
var_export($a);
?>

The above example will output:

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

$b = 3.1;
$v = var_export($b, true);
echo $v;

?>

The above example will output:

3.1

Example #2 Exporting stdClass (since PHP 7.3.0)

<?php
$person = new stdClass;
$person->name = 'ElePHPant ElePHPantsdotter';
$person->website = 'https://php.net/elephpant.php';

var_export($person);

The above example will output:

(object) array(
   'name' => 'ElePHPant ElePHPantsdotter',
   'website' => 'https://php.net/elephpant.php',
)

Example #3 Exporting classes

<?php
class A { public $var; }
$a = new A;
$a->var = 5;
var_export($a);
?>

The above example will output:

A::__set_state(array(
   'var' => 5,
))

Example #4 Using __set_state()

<?php
class A
{
    public $var1;
    public $var2;

    public static function __set_state($an_array)
    {
        $obj = new A;
        $obj->var1 = $an_array['var1'];
        $obj->var2 = $an_array['var2'];
        return $obj;
    }
}

$a = new A;
$a->var1 = 5;
$a->var2 = 'foo';

eval('$b = ' . var_export($a, true) . ';'); // $b = A::__set_state(array(
                                            //    'var1' => 5,
                                            //    'var2' => 'foo',
                                            // ));
var_dump($b);
?>

The above example will output:

object(A)#2 (2) {
  ["var1"]=>
  int(5)
  ["var2"]=>
  string(3) "foo"
}

Notes

Note:

Variables of type resource couldn't be exported by this function.

Note:

var_export does not handle circular references as it would be close to impossible to generate parsable PHP code for that. If you want to do something with the full representation of an array or object, use serialize.

Warning

Prior to PHP 8.2.0, when var_export exports objects, the leading backslash is not included in the class name of namespaced classes for maximum compatibility.

Note:

To be able to evaluate the PHP generated by var_export, all processed objects must implement the magic __set_state method. The only exception is stdClass, which is exported using an array cast to an object.

See Also

  • print_r
  • serialize
  • var_dump