03 August 2010 ~ 1 Comment

var_export in PHP 5

So var_export(mixed $expression [, bool $return = false ] ) outputs or returns a parsable string representation of a variable, it gets structured information about the given variable. It is similar to var_dump() with one exception: the returned representation is valid PHP code.

It’s quite simple to use, below is an example of how to use it.

1
2
3
4
5
6
class A { 
     public $var; 
}
$a = new A;
$a->var = 5;
var_export($a);

The output from the above code would be…

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

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().

Tags:

One Response to “var_export in PHP 5”

  1. Greg Winn 3 August 2010 at 11:06 pm Permalink

    Caution! var_export will add a backslash to single quotes (‘).

    You may want to use stripslashes() to remove the mysteriously added backslashes.