PHP Function Reference

PHP var_export() Function



The PHP var_export() function returns structured information about the given variable. This function is similar to var_dump() except the returned representation is valid PHP code.

Syntax

var_export(variable, return)

Parameters

variable Required. Specify the variable to export.
return Optional. If set to true, this function returns the variable representation instead of outputting it. Default is false

Return Value

If return is set to true, the function returns the variable representation. Otherwise, it returns null.

Example:

The example below shows the usage of var_export() function.

<?php
$x = array(10, 20, array("a", "b"));
var_export($x);

echo "\n\n";

$y = array(10=>"Red", 20=>"Green", 30=>"Blue",
           40=>array("Black", "White"));
var_export($y);
?>

The output of the above code will be:

array (
  0 => 10,
  1 => 20,
  2 => 
  array (
    0 => 'a',
    1 => 'b',
  ),
)

array (
  10 => 'Red',
  20 => 'Green',
  30 => 'Blue',
  40 => 
  array (
    0 => 'Black',
    1 => 'White',
  ),
)

Example:

Consider one more example where this function is used with return parameter which is set to true.

<?php
$x = "Hello";
$y = var_export($x, true);
echo $y;

echo "\n";

$p = 10.5;
$q = var_export($p, true);
echo $q;
?>

The output of the above code will be:

'Hello'
10.5

❮ PHP Variable Handling Reference