401 of 410 menu

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

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

Example

Let's output an array as valid PHP code:

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

Code execution result:

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

Example

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

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

Code execution result:

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

Example

Exporting a string variable:

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

Code execution result:

'test'

See Also

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