In PHP, var_export() outputs or returns a string of valid PHP code that represents a value. If you run that code, you get the same value back.
It is useful for debugging and for generating PHP files, such as cache or configuration files. Strings are shown with quotes, true and false are written as words, and arrays are written in the array ( ... ) format. With the second argument set to true, it returns the code instead of printing it.
Syntax
Call var_export() with the values you need. Start with this line.
PHP
var_export($value);
Call
What it does
var_export('hello')
Prints 'hello' with the quotes, so it is valid PHP code.
var_export([1, 2])
Prints the array in array ( ... ) format.
var_export($value, true)
Does not print anything. It returns the code as a string.
Unlike print_r(), var_export() shows real PHP syntax, so true is written as true and strings are quoted. A float with no decimals gets a .0 added, so 1.0 is exported as 1.0.
The settings array holds two values. We want to store it as PHP code.
var_export($settings, true) returns the code as a string. Nothing is printed at that point.
The dot joins it between '<?php return ' and a semicolon. The result is a complete PHP file in one string.
Saving this text to a file and loading it with include would return the array again.
Frequently Asked Questions
It outputs or returns a string of valid PHP code that represents a value. The code is written so that running it would recreate the same value.
var_dump() shows types and sizes for debugging, such as int(5). var_export() shows real PHP code, such as 5 or 'text', which you can paste into a script or save to a file.
Pass true as the second argument, like var_export($value, true). It then returns the text instead of printing it.
print_r() is meant to be read by people and hides types. var_export() produces valid PHP code, with quotes around strings and the words true, false and NULL.
Yes, but the result calls the static method __set_state() of the class, or uses the (object) array() form for stdClass. The class must define __set_state() for the code to rebuild the object.