var_export

(PHP 4 >= 4.2.0, PHP 5)

var_export -- 输出或返回一个变量的字符串表示

描述

mixed var_export ( mixed expression [, bool return] )

此函数返回关于传递给该函数的变量的结构信息,它和 var_dump() 类似,不同的是其返回的表示是合法的 PHP 代码。

您可以通过将函数的第二个参数设置为 TRUE,从而返回变量的表示。

比较 var_export()var_dump().

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

/* 输出:
array (
  0 => 1,
  1 => 2,
  2 =>
  array (
    0 => 'a',
    1 => 'b',
    2 => 'c',
  ),
)
*/

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

/* 输出:
3.1
*/
?>
</pre>


add a note add a note User Contributed Notes
Zorro
05-Sep-2005 02:24
This function can't export EVERYTHING. Moreover, you can have an error on an simple recursive array:

$test = array();
$test["oops"] = & $test;

echo var_export($test);

=>

Fatal error:  Nesting level too deep - recursive dependency? in ??.php on line 59
linus at flowingcreativity dot net
05-Jul-2005 12:50
<roman at DIESPAM dot feather dot org dot ru>, your function has inefficiencies and problems. I probably speak for everyone when I ask you to test code before you add to the manual.

Since the issue of whitespace only comes up when exporting arrays, you can use the original var_export() for all other variable types. This function does the job, and, from the outside, works the same as var_export().

<?php

function var_export_min($var, $return = false) {
   if (
is_array($var)) {
      
$toImplode = array();
       foreach (
$var as $key => $value) {
          
$toImplode[] = var_export($key, true).'=>'.var_export_min($value, true);
       }
      
$code = 'array('.implode(',', $toImplode).')';
       if (
$return) return $code;
       else echo
$code;
   } else {
       return
var_export($var, $return);
   }
}

?>
roman at DIESPAM dot feather dot org dot ru
19-Mar-2005 05: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
25-Nov-2004 03: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
11-Jun-2004 12: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
php_manual_note at bigredspark dot com
16-Oct-2003 03: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...