PHP 8.4.1 Released!

var_export

(PHP 4 >= 4.2.0, PHP 5, PHP 7, PHP 8)

var_exportLiefert den Inhalt einer Variablen als parsbaren PHP-Code

Beschreibung

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

var_export() gibt strukturierte Informationen zum Inhalt der übergebenen Variable zurück. Das Verhalten ist ähnlich dem der Funktion var_dump(), allerdings ist hier das Ergebnis valider PHP-Code mit dem sich der Inhalt der Variable wiederherstellen lässt.

Parameter-Liste

value

Die zu exportierende Variable.

return

Wird dieser Parameter auf true gesetzt, so wird das Ergebnis nicht direkt ausgegeben, sondern als String zurückgegeben.

Rückgabewerte

Gibt die Darstellung der Variablen als PHP-Code zurück, wenn der return-Parameter genutzt wird und als Wert true ergibt. Andernfalls gibt die Funktion null zurück.

Changelog

Version Beschreibung
8.2.0 Exportierte Klassennamen sind nun vollständig qualifiziert; zuvor wurde der vorangestellte Backslash weggelassen.
7.3.0 stdClass-Objekte werden nun als ein Array exportiert, das in ein Objekt umgewandelt wird ((object) array( ... )), anstatt die nicht existente Methode stdClass::__setState() zu verwenden. Der praktische Effekt ist, dass stdClass nun exportierbar ist, und der erzeugte Code sogar mit früheren Versionen von PHP funktioniert.

Beispiele

Beispiel #1 var_export()-Beispiele

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

Das oben gezeigte Beispiel erzeugt folgende Ausgabe:

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

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

?>

Das oben gezeigte Beispiel erzeugt folgende Ausgabe:

3.1

Beispiel #2 Export von stdClass (seit PHP 7.3.0)

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

var_export($person);

Das oben gezeigte Beispiel erzeugt folgende Ausgabe:

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

Beispiel #3 Export von Klassen

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

Das oben gezeigte Beispiel erzeugt folgende Ausgabe:

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

Beispiel #4 Nutzung von __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);
?>

Das oben gezeigte Beispiel erzeugt folgende Ausgabe:

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

Anmerkungen

Hinweis:

Variablen vom Typ resource können von dieser Funktion nicht exportiert werden.

Hinweis:

var_export() ist nicht in der Lage, zirkuläre Referenzen aufzulösen, da es annähernd unmöglich ist, hierfür gültigen PHP-Code zu erzeugen. In solchen Fällen empfiehlt sich stattdessen die Nutzung der serialize() für eine vollständige Darstellung von entsprechenden Arrays oder Objekten.

Warnung

Vor PHP 8.2.0 war beim Export von Objekten mittels var_export() der führende Backslash zwecks größtmöglicher Kompatibilität nicht im Namen von Klassen in Namensräumen enthalten.

Hinweis:

Um das von var_export() erzeugte PHP ausführen zu können, müssen alle verarbeiteten Objekte die magische Methode __set_state implementieren. Die einzige Ausnahme ist stdClass, die unter Verwendung eines Arrays, das zu einem Objekt umgewandelt wird, exportiert wird.

Siehe auch

  • print_r() - Gibt Variablen-Informationen in lesbarer Form aus
  • serialize() - Erzeugt eine speicherbare Repräsentation eines Wertes
  • var_dump() - Gibt alle Informationen zu einer Variablen aus

add a note

User Contributed Notes 21 notes

up
32
steven at nevvix dot com
5 years ago
I improved my previous varexport().

<?php
/**
* PHP var_export() with short array syntax (square brackets) indented 2 spaces.
*
* NOTE: The only issue is when a string value has `=>\n[`, it will get converted to `=> [`
* @link https://www.php.net/manual/en/function.var-export.php
*/
function varexport($expression, $return=FALSE) {
$export = var_export($expression, TRUE);
$patterns = [
"/array \(/" => '[',
"/^([ ]*)\)(,?)$/m" => '$1]$2',
"/=>[ ]?\n[ ]+\[/" => '=> [',
"/([ ]*)(\'[^\']+\') => ([\[\'])/" => '$1$2 => $3',
];
$export = preg_replace(array_keys($patterns), array_values($patterns), $export);
if ((bool)
$return) return $export; else echo $export;
}

$array = [
'str' => 'Test
spaces'
,
0 => 33,
1 => TRUE,
[
3,4,'d',[]],
'arr' => [
'text with spaces' => '[Tes\'t"s":
=> [
=>
[
{
spaces'
,
],
"str2" => "Test's'
} spaces"
,
'arr2' => [
'text with spaces' => [
'arr3' => [
'text with spaces' => 'Te": "st \' => [
spaces'
,
],
],
],
];
varexport($array);
// Result:
```
[
'str' => 'Test
spaces',
0 => 33,
1 => true,
2 => [
0 => 3,
1 => 4,
2 => 'd',
3 => [
],
],
'arr' => [
'text with spaces' => '[Tes\'t"s":
=> [
=> [
{
spaces',
],
'str2' => 'Test\'s\'
} spaces',
'arr2' => [
'text with spaces' => [
'arr3' => [
'text with spaces' => 'Te": "st \' => [
spaces',
],
],
],
]
```
NOTE: The only issue is when a string value has `=>\n[`, it will get converted to `=> [`
up
27
steven at nevvix dot com
6 years ago
/**
* var_export() with square brackets and indented 4 spaces.
*/
<?php
function varexport($expression, $return=FALSE) {
$export = var_export($expression, TRUE);
$export = preg_replace("/^([ ]*)(.*)/m", '$1$1$2', $export);
$array = preg_split("/\r\n|\n|\r/", $export);
$array = preg_replace(["/\s*array\s\($/", "/\)(,)?$/", "/\s=>\s$/"], [NULL, ']$1', ' => ['], $array);
$export = join(PHP_EOL, array_filter(["["] + $array));
if ((bool)
$return) return $export; else echo $export;
}
up
15
Mark P
8 years ago
It doesn't appear to be documented, but the behaviour of `var_export()` changed in PHP 7.

Previously, `var_export(3.)` returned "3", now it returns "3.0".
up
21
chudinov at yahoo dot com
10 years ago
Looks like since version 5.4.22 var_export uses the serialize_precision ini setting, rather than the precision one used for normal output of floating-point numbers.
As a consequence since version 5.4.22 for example var_export(1.1) will output 1.1000000000000001 (17 is default precision value) and not 1.1 as before.

<?php
//ouput 1.1000000000000001
var_export(1.1)
?>
up
8
4n4jmza02 at sneakemail dot com
14 years ago
I learned the hard way that if var_export encounters a resource handle it exports it as "NULL", even if it is a valid handle. The documentation states that a handle cannot be exported, but it does not describe what happens if you try to do so anyway.

I had been using var_export in some debugging code while tracing a problem with a resource handle not being generated and ended up thinking that null handles were still being generated long after the problem had been fixed.
up
12
dan at coders dot co dot nz
10 years ago
I found that my complex type was exporting with
stdClass::__set_state()
in places. Not only was that strange and messy, it cannot be eval()-ed back in at all. Fatal error. Doh!

However a quick string-replace tidy-up of the result rendered it valid again.

$macro = var_export($data, TRUE);
$macro = str_replace("stdClass::__set_state", "(object)", $macro);
$macro = '$data = ' . $macro . ';';

And now the string I output *can* be evaluated back in again.
up
10
laszlo dot heredy at gmail dot com
14 years ago
Try this function instead of var_export($GLOBALS) or var_dump($GLOBALS) when all you want to know is the values of the variables you set on the current page.

<?php
function globalvars(){
$result=array();
$skip=array('GLOBALS','_ENV','HTTP_ENV_VARS',
'_POST','HTTP_POST_VARS','_GET',
'HTTP_GET_VARS',
'_COOKIE',
'HTTP_COOKIE_VARS','_SERVER',
'HTTP_SERVER_VARS',
'_FILES','HTTP_POST_FILES',
'_REQUEST','HTTP_SESSION_VARS',
'_SESSION');
foreach(
$GLOBALS as $k=>$v)
if(!
in_array($k,$skip))
$result[$k]=$v;
return
$result;
}
//functionglobalvars

var_export(globalvars());
?>
up
12
linus at flowingcreativity dot net
19 years ago
<roman at DIESPAM dot feather dot org dot ru>, your function has inefficiencies and problems. I probably speak for everyone when I ask you to test code before you add to the manual.

Since the issue of whitespace only comes up when exporting arrays, you can use the original var_export() for all other variable types. This function does the job, and, from the outside, works the same as var_export().

<?php

function var_export_min($var, $return = false) {
if (
is_array($var)) {
$toImplode = array();
foreach (
$var as $key => $value) {
$toImplode[] = var_export($key, true).'=>'.var_export_min($value, true);
}
$code = 'array('.implode(',', $toImplode).')';
if (
$return) return $code;
else echo
$code;
} else {
return
var_export($var, $return);
}
}

?>
up
5
NitPicker
11 years ago
When it comes to HTML output (as discussed below), it's all fun and games until someone pokes their eye out with a "<".

Surround it with "<pre>", but do remember to wrap it in htmlspecialchars() as well.
up
3
beverasrilakshmi at gmail dot com
5 years ago
Just for fun, trying to understand the definition of "returns parsable string"....any type of variable passed to var_export, the return value will be a typecasted as string...

<?php

$var
= 1;
var_dump($var); //type is int as expected
echo "<br>";
$var_after_export = var_export($var,true); //returning $var will now makes it a string
var_dump($var_after_export);

?>
up
5
john dot risken at gmail dot com
14 years ago
I didn't see this simple little item anywhere in the user notes. Maybe I'm blind!

Anyway, var_export and print_r both use spaces and carriage returns for formatting. Sent to an html page, most of the formatting is lost. This simple function prints a nicely formatted array to an html screen:

<?php
function pretty_var($myArray){
print
str_replace(array("\n"," "),array("<br>","&nbsp;"), var_export($myArray,true))."<br>";
}
?>
up
7
Glen
17 years ago
Like previously reported, i find var_export() frustrating when dealing with recursive structures. Doing a :

<?php
var_export
($GLOBALS);
?>

fails. Interestingly, var_dump() has some logic to avoid recursive references. So :

<?php
var_dump
($GLOBALS);
?>

works (while being more ugly). Unlike var_export(), var_dump() has no option to return the string, so output buffering logic is required if you want to direct the output.
up
5
ravenswd at gmail dot com
15 years ago
(This replaces my note of 3-July-2009. The original version produced no output if a variable contained an empty array, or an array consisting only of empty arrays. For example, $bigarray['x'] = array(); Also, I have added a second version of the function.)

The output can be difficult to decipher when looking at an array with many levels and many elements on each level. For example:

<?php
print ('$bigarray = ' . var_export($bigarray, true) . "\n");
?>

will return:

$bigarray = array(
... (500 lines skipped) ...
'mod' => 'charlie',

Whereas the routine below can be called with:

<?php
recursive_print
('$bigarray', $bigarray);
?>

and it will return:

$bigarray = array()
... (500 lines skipped) ...
$bigarray['foo']['bar']['0']['somethingelse']['mod'] = 'charlie'

Here's the function:

<?php
function recursive_print ($varname, $varval) {
if (!
is_array($varval)):
print
$varname . ' = ' . $varval . "<br>\n";
else:
print
$varname . " = array()<br>\n";
foreach (
$varval as $key => $val):
recursive_print ($varname . "['" . $key . "']", $val);
endforeach;
endif;
}
?>

For those who want a version that produces valid PHP code, use this version:

<?php
function recursive_print ($varname, $varval) {
if (!
is_array($varval)):
print
$varname . ' = ' . var_export($varval, true) . ";<br>\n";
else:
print
$varname . " = array();<br>\n";
foreach (
$varval as $key => $val):
recursive_print ($varname . "[" . var_export($key, true) . "]", $val);
endforeach;
endif;
}
?>

If your output is to a text file and not an HTML page, remove the <br>s.
up
4
sergei dot solomonov at gmail dot com
12 years ago
<?php
$closure
= function(){};

var_export($closure);

// output: Closure::__set_state(array())
?>
up
4
jodybrabec at gmail dot com
12 years ago
WORKAROUND for error "Nesting level too deep - recursive dependency":
ob_start();
var_dump($GLOBALS);
$dataDump = ob_get_clean();
echo $dataDump;
up
4
Anonymous
13 years ago
There is an even simpler way to have clean output from var_export and print_r in html pages:

<?php
function pretty_var($myArray)
{
echo
"<pre>";
var_export($myArray);
echo
"</pre>";
}
?>
up
2
stangelanda at arrowquick dot com
17 years ago
I have been looking for the best method to store data in cache files.

First, I've identified two limitations of var_export verus serialize. It can't store internal references inside of an array and it can't store a nested object or an array containing objects before PHP 5.1.0.

However, I could deal with both of those so I created a benchmark. I used a single array containing from 10 to 150 indexes. I've generate the elements' values randomly using booleans, nulls, integers, floats, and some nested arrays (the nested arrays are smaller averaging 5 elements but created similarly). The largest percentage of elements are short strings around 10-15 characters. While there is a small number of long strings (around 500 characters).

Benchmarking returned these results for 1000 * [total time] / [iterations (4000 in this case)]

serialize 3.656, 3.575, 3.68, 3.933, mean of 3.71
include 7.099, 5.42, 5.185, 6.076, mean of 5.95
eval 5.514, 5.204, 5.011, 5.788, mean of 5.38

Meaning serialize is around 1 and a half times faster than var_export for a single large array. include and eval were consistently very close but eval was usually a few tenths faster (eval did better this particular set of trials than usual). An opcode cache like APC might make include faster, but otherwise serialize is the best choice.
up
2
paul at worldwithoutwalls dot co dot uk
19 years ago
var_export() differs from print_r() for variables that are resources, with print_r() being more useful if you are using the function for debugging purposes.
e.g.
<?php
$res
= mysql_connect($dbhost, $dbuser, $dbpass);
print_r($res); //output: Resource id #14
var_export($res); //output: NULL
?>
up
1
me at peterkooi dot com
14 days ago
A small function for exporting variables as string, supporting nested arrays, with indented output, blockquoted and with double quotes, that can be pasted back in code.

<?php
function dump($value) {
function
_dump($value, $indent = 0) {
if (!
is_array($value)) return json_encode($value, JSON_NUMERIC_CHECK);
foreach(
$value as $key => $item) $result .= (ifset($result) ? ",\r\n" . str_repeat(" ", $indent + 2) : "") . json_encode($key) . " => " . _dump($item, $indent + 2);
return
"[\r\n" . str_repeat(" ", $indent + 2) . "$result\r\n" . str_repeat(" ", $indent) . "]";
}
return
"<pre>" . htmlspecialchars(_dump($value)) . "</pre>";
}
up
1
php_manual_note at bigredspark dot com
21 years ago
[john holmes]
True, but that method would require you to open and read the file into a variable and then unserialize it into another variable.

Using a file created with var_export() could simply be include()'d, which will be less code and faster.

[kaja]
If you are trying to find a way to temporarily save variables into some other file, check out serialize() and unserialize() instead - this one is more useful for its readable property, very handy while debugging.

[original post]
If you're like me, you're wondering why a function that outputs "correct PHP syntax" is useful. This function can be useful in implementing a cache system. You can var_export() the array into a variable and write it into a file. Writing a string such as

<?php
$string
= '<?php $array = ' . $data . '; ?>';
?>

where $data is the output of var_export() can create a file that can be easily include()d back into the script to recreate $array.

The raw output of var_export() could also be eval()d to recreate the array.

---John Holmes...
up
0
rarioj at gmail dot com
15 years ago
NOTE: If an object Foo has __set_state() method, but if that object contains another object Bar with no __set_state() method implemented, the resulting PHP expression will not be eval()-able.

This is an example (object Test that contains an instance of Exception).

<?php

class Test
{
public
$one;
public
$two;
public function
__construct($one, $two)
{
$this->one = $one;
$this->two = $two;
}
public static function
__set_state(array $array)
{
return new
self($array['one'], $array['two']);
}
}

$test = new Test('one', new Exception('test'));

$string = var_export($test, true);

/* $string =
Test::__set_state(array(
'one' => 'one',
'two' =>
Exception::__set_state(array(
'message' => 'test',
'string' => '',
'code' => 0,
'file' => 'E:\\xampp\\htdocs\\test.Q.php',
'line' => 35,
'trace' =>
array (
),
'previous' => NULL,
)),
))
*/

eval('$test2 = '.$string.';'); // Fatal error: Call to undefined method Exception::__set_state

?>

So avoid using var_export() on a complex array/object that contains other objects. Instead, use serialize() and unserialize() functions.

<?php

$string
= 'unserialize('.var_export(serialize($test), true).')';

eval(
'$test2 = '.$string.';');

var_dump($test == $test2); // bool(true)

?>
To Top