Analysis of the difference between var_export and var_dump in php_PHP tutorial

WBOY
Release: 2016-07-21 15:34:02
Original
945 people have browsed it

a var_dump
(PHP 3 >= 3.0.5, PHP 4, PHP 5)
var_dump -- Print information about variables
Description
void var_dump ( mixed expression [, mixed expression [, ...]] )

This function displays structural information about one or more expressions, including the type and value of the expression. Arrays will expand values ​​recursively, showing their structure through indentation.

Copy code The code is as follows:

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

The output format is as follows:
Copy code The code 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 -- Export or return a string representation of a variable
Description
mixed var_export ( mixed expression [, bool return] )

This function returns structural information about the variables passed to the function. It is similar to var_dump(), except that the representation returned is legal PHP code.
You can return a representation of a variable by setting the second parameter of the function to TRUE.
EG:
var_export(array('a','b',array('aa','bb','cc'))) This is no different from VAR_DUMP;

$ var =var_export(array('a','b',array('aa','bb','cc')),TRUE), after adding TRUE, it will not be printed out, but a variable will be given , so that it can be output directly;
echo $var; The output form at this time is similar to that printed by var_dump().
EG2
Copy code The code is as follows:

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

The output format is as follows:
Copy code The code is as follows:

array (
'name' => 'abc',
'job' => 'programmer',
'a' =>
array (
0 => 'aa ',
1 => 'cc',
2 => 'bb',
),
)


The following are Supplementary information:
error_log(var_export(yblog_mspconfiginit("ratings"),true));
Cause of the problem
var_export must return legal php code. In other words, the code returned by var_export can Assign a variable directly as php code. And this variable will get the same type of value as var_export. However, when the variable type is resource, it cannot be copied simply. Therefore, when the variable of var_export is of resource type, var_export will return NULL.

Problem discovery
When tracking yratings_get_targets,
Copy code The code is as follows:

error_log(var_export(yblog_mspconfiginit("ratings"),true)); always prints The return value of yblog_mspconfiginit("ratings") was NULL

, which led me to think that the connection to the DB could not be established, and I went down the wrong path for a day.

Finally, I discovered that this is one of the differences between var_export and var_dump

This is:

Cause of the problem
var_export must return legal php code, that is , the code returned by var_export can be directly used as PHP code to assign a variable. And this variable will get the same type of value as var_export

However, when the variable type is resource, it cannot be simply copied. Therefore, when the variable of var_export is of resource type, var_export will Return NULL
Instance
Copy code The code is as follows:

$res = yblog_mspconfiginit("ratings");
var_dump($res);
var_export($res);

Result:
Copy code The code is as follows:

resource(1) of type (yahoo_yblog)

NULL Another example:
Copy code The code is as follows:

$res = fopen('status.html', 'r');
var_dump($res);
var_export($res);

Result:
Copy code The code is as follows:

resource(2) of type (stream)
NULL

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/322468.htmlTechArticlea var_dump (PHP 3 = 3.0.5, PHP 4, PHP 5) var_dump -- Print information about variables Description void var_dump ( mixed expression [, mixed expression [, ...]] ) This function displays information about a or...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template