search for in the  
<var_dumpvpopmail>
Last updated: Thu, 19 May 2005

var_export

(PHP 4 >= 4.2.0, PHP 5)

var_export -- Outputs or returns a parsable string representation of a variable

Description

mixed var_export ( mixed expression [, bool return] )

var_export() gets structured information about the given variable. It is similar to var_dump() with one exception: the returned representation is valid PHP code.

Parameters

expression

The variable you want to export.

return

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

Return Values

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

Examples

Example 1. var_export() Examples

<?php
$a
= array (1, 2, array ("a", "b", "c"));
var_export($a);
?>

The above example will output:

array (
  0 => 1,
  1 => 2,
  2 => 
  array (
    0 => 'a',
    1 => 'b',
    2 => 'c',
  ),
)
<?php

$b
= 3.1;
$v = var_export($b, true);
echo
$v;

?>

The above example will output:

3.1



User Contributed Notes
var_export
roman at DIESPAM dot feather dot org dot ru
18-Mar-2005 03:46
Function that exports variables without adding any junk to the resulting string:
<?php
function encode($var){
   if (
is_array($var)) {
      
$code = 'array(';
       foreach (
$var as $key => $value) {
          
$code .= "'$key'=>".encode($value).',';
       }
      
$code = chop($code, ','); //remove unnecessary coma
      
$code .= ')';
       return
$code;
   } else {
       if (
is_string($var)) {
           return
"'".$var."'";
       } elseif (
is_bool($code)) {
           return (
$code ? 'TRUE' : 'FALSE');
       } else {
           return
'NULL';
       }
   }
}

function
decode($string){
   return eval(
'return '.$string.';');
}
?>
The resulting string can sometimes be smaller, that output of serialize(). May be useful for storing things in the database.
paul at worldwithoutwalls dot co dot uk
24-Nov-2004 01:22
var_export() differs from print_r() for variables that are resources, with print_r() being more useful if you are using the function for debugging purposes.
e.g.
<?php
$res
= mysql_connect($dbhost, $dbuser, $dbpass);
print_r($res); //output: Resource id #14
var_export($res); //output: NULL
?>
aidan at php dot net
10-Jun-2004 11:53
This functionality is now implemented in the PEAR package PHP_Compat.

More information about using this function without upgrading your version of PHP can be found on the below link:

http://pear.php.net/package/PHP_Compat
webmaster at huinca dot net
26-Oct-2003 01:11
(Regarding a previous post in this page)

Please keep in mind that $GLOBALS is a quite unique type of array, where nesting level are infinite because $GLOBALS['GLOBALS'] is exactly $GLOBALS (and recursively). Thus,

// Never try these ones!
var_export($GLOBALS);
var_dump($GLOBALS);

yields a nesting level very, very deep. As deep as you want :) So, to check recursivity please choose any other variable.
php_manual_note at bigredspark dot com
16-Oct-2003 02:43
[john holmes]
True, but that method would require you to open and read the file into a variable and then unserialize it into another variable.

Using a file created with var_export() could simply be include()'d, which will be less code and faster.

[kaja]
If you are trying to find a way to temporarily save variables into some other file, check out serialize() and unserialize() instead - this one is more useful for its readable property, very handy while debugging.

[original post]
If you're like me, you're wondering why a function that outputs "correct PHP syntax" is useful. This function can be useful in implementing a cache system. You can var_export() the array into a variable and write it into a file. Writing a string such as

<?php
$string
= '<?php $array = ' . $data . '; ?>';
?>

where $data is the output of var_export() can create a file that can be easily include()d back into the script to recreate $array.

The raw output of var_export() could also be eval()d to recreate the array.

---John Holmes...

<var_dumpvpopmail>
 Last updated: Thu, 19 May 2005
Copyright © 2001-2005 The PHP Group
All rights reserved.
This unofficial mirror is operated at: The Server Pages
Last updated: Thu May 19 17:35:34 2005 CDT