Analysis of the difference between var_export and var_dump in PHP

  • 2020-03-31 21:06:03
  • OfStack

A var_dump
(PHP 3 > = 3.0.5, PHP 4, PHP 5)
Var_dump - prints information about a variable
describe
Void var_dump (mixed expression [, mixed expression [,...]])

This function displays information about the structure of one or more expressions, including the type and value of the expression. An array expands the value recursively, showing its structure by indentation.

 
$data = array ('name' => 'abc', 'job' => 'programmer','a'=>array('aa','cc','bb')); 
$data = var_dump($data,TRUE); 
echo $data; 

The output form is as follows:
 
array(3) { 
["name"]=> 
string(3) "abc" 
["job"]=> 
string(10) "programmer" 
["a"]=> 
array(3) { 
[0]=> 
string(2) "aa" 
[1]=> 
string(2) "cc" 
[2]=> 
string(2) "bb" 
} 
} 
bool(true) 

Two var_export
(PHP 4 > = 4.2.0, PHP 5)
Var_export - outputs or returns a string representation of a variable
describe
Mixed var_export (mixed expression [, bool return])

This function returns information about the structure of the variable passed to it, similar to var_dump() except that the representation it returns is legitimate PHP code.
You can return a representation of the variable by setting the second parameter of the function to TRUE.
EG:
Var_export (array (' a ', 'b', array (' aa ', 'bb', 'cc'))) this with VAR_DUMP makes no difference;

Var =var_export(array('a','b',array('aa','bb','cc')
Echo $var. The output is similar to what var_dump() prints.
EG2
 
$data = array ('name' => 'abc', 'job' => 'programmer','a'=>array('aa','cc','bb')); 
$data = var_export($data,TRUE); 
echo $data; 

The output form is as follows:
 
array ( 
'name' => 'abc', 
'job' => 'programmer', 
'a' => 
array ( 
0 => 'aa', 
1 => 'cc', 
2 => 'bb', 
), 
) 


The following is supplementary information:
Error_log (var_export (yblog_mspconfiginit (" ratings "), true));
Question why
Var_export must return valid PHP code, that is, the code returned by var_export can be directly assigned to a variable as PHP code. This variable gets the value of the same type as var_export. However, it is not easy to copy when the variable type is resource, so var_export returns NULL when the variable of var_export is of resource type.

The problem found
When tracking yratings_get_targets,
 
error_log(var_export(yblog_mspconfiginit("ratings"),true)); Always printing out yblog_mspconfiginit( " ratings " ) The return is NULL 

As a result, I thought that I could not establish a connection with DB, so I went the wrong way for a day.

It turns out that this is one of the differences between var_export and var_dump

This is:

Question why
Var_export must return valid PHP code, that is, the code returned by var_export can be directly assigned to a variable as PHP code. This variable gets the value of the same type as var_export

However, it is not easy to copy when the variable type is resource, so var_export returns NULL when the variable of var_export is of resource type
The instance
 
$res = yblog_mspconfiginit("ratings"); 
var_dump($res); 
var_export($res); 

Results:
 
resource(1) of type (yahoo_yblog) 

NULL is another example:
 
$res = fopen('status.html', 'r'); 
var_dump($res); 
var_export($res); 

Results:
 
resource(2) of type (stream) 
NULL 


Related articles: