var_export

Outputs or returns a parsable string representation of a variable

Syntax

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

Parameters

value

The variable to export.

return

If used and set to TRUE, var_export() will return the variable representation instead of outputting it.

Return

Returns the variable representation when the return parameter is used and evaluates to TRUE. Otherwise, this function will return NULL.

Examples

value | bool

<?

$value = false;

var_export($value);

?>
false

value | int

<?

$value = 0;

var_export($value);

?>
0

value | float

<?

$value = 0.0;

var_export($value);

?>
0.0

value | string

<?

$value = "0";

var_export($value);

?>
'0'

value | array

<?

$value = array(0, 1, array(0, 1));

var_export($value);

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

value | object | standard class

<?

$value = new stdclass;
$value->var = 0;

var_export($value);

?>
(object) array(
   'var' => 0,
)

value | object | custom class

<?

class myclass
{
    public $var;
}

$value = new myclass;
$value->var = 0;

var_export($value);

?>
myclass::__set_state(array(
   'var' => 0,
))

return | false

<?

class myclass
{
    public $var;
}

$value = new myclass;
$value->var = 0;
$return = false;

$output = var_export($value, $return);

echo PHP_EOL . "output:" . PHP_EOL;
var_export($output);

?>
myclass::__set_state(array(
   'var' => 0,
))
output:
NULL

return | true

<?

class myclass
{
    public $var;
}

$value = new myclass;
$value->var = 0;
$return = true;

$output = var_export($value, $return);

echo "output:" . PHP_EOL . $output;

?>
output:
myclass::__set_state(array(
   'var' => 0,
))

Internal

print_r

var_dump

External

PHP

Home Menu