The var_export Function

The var_export function outputs or returns information about a variable in a format suitable for use in PHP code. The first parameter takes the variable to export, the second - a flag to return the result instead of outputting it.

Syntax

phpvar_export(mixed $value, bool $return = false);

Example

Let's output an array as valid PHP code:

php<?php
  $arr = [1, 2, 3];
  var_export($arr);
?>

Code execution result:

phparray (
  0 => 1,
  1 => 2,
  2 => 3,
)

Example

Get the string representation of a variable without output, using the second parameter:

php<?php
  $arr = ['a', 'b', 'c'];
  $res = var_export($arr, true);
  echo $res;
?>

Code execution result:

php[
  0 => 'a',
  1 => 'b',
  2 => 'c',
]

Example

Exporting a string variable:

php<?php
  $str = 'test';
  var_export($str);
?>

Code execution result:

php'test'

See Also

  • the print_r function,
    which outputs information about a variable
  • the var_dump function,
    which outputs structured information about a variable