PHP var_export() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Variables

Definition

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);
CallWhat 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.

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

Parameters

ParameterDescription
valueRequired. The value to export. It can be of any type except a resource.
returnOptional. When true, var_export() returns the code as a string instead of printing it. The default is false.

Return Value

Return ValueDescription
nullReturned after printing, when return is false (the default).
stringThe PHP code that represents the value, only when return is set to true.

PHP Examples

Example 1 — Exporting Simple Values

Export an integer, a string, a boolean, null and a float, and notice that each result is valid PHP code.

PHP
<?php
var_export(42);
echo PHP_EOL;

var_export('hello');
echo PHP_EOL;

var_export(true);
echo PHP_EOL;

var_export(null);
echo PHP_EOL;

var_export(1.0);
echo PHP_EOL;

How It Works

  1. The integer 42 is shown as 42. Numbers look the same as in your code.
  2. The string is shown with single quotes. 'hello' is exactly how you would write it in PHP.
  3. true is written as the word true. null is written in capitals as NULL.
  4. The float 1. 0 keeps its .0. This makes sure the value stays a float when the code is run again.

Example 2 — Exporting an Array with Quotes

Export an array that has a boolean, a number and a string with an apostrophe. Quotes inside the text are escaped for you.

PHP
<?php
$config = ['debug' => true, 'level' => 3, 'owner' => "O'Brien"];

var_export($config);
echo PHP_EOL;

How It Works

  1. The array is written in the array ( . .. ) format. Each item has a key, an arrow and a value.
  2. Keys and string values are quoted. true and the number 3 are written without quotes.
  3. The apostrophe in O'Brien is escaped with a backslash. This keeps the exported text valid PHP.
  4. A comma follows every item, even the last one. PHP allows this, and it is easy to copy and paste.

Example 3 — Generating a PHP Config File

Use the return option to build a line of PHP code that you could save into a config file.

PHP
<?php
$settings = ['theme' => 'dark', 'size' => 12];

$code = '<?php return ' . var_export($settings, true) . ';';
echo $code, PHP_EOL;

How It Works

  1. The settings array holds two values. We want to store it as PHP code.
  2. var_export($settings, true) returns the code as a string. Nothing is printed at that point.
  3. The dot joins it between '<?php return ' and a semicolon. The result is a complete PHP file in one string.
  4. 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.

Back to Variable Functions

Browse all PHP variable function tutorials.

Variable functions →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful