Analysis of the difference between var_export and var_dump in php

小云云
Release: 2023-03-21 12:00:01
Original
1869 people have browsed it

This article mainly shares with you the analysis of the difference between var_export and var_dump in php, hoping to help everyone.

var_dump (PHP 3 >= 3.0.5, PHP 4, PHP 5)

var_dump -- Print related information about variables
Description

void var_dump ( mixed expression [, mixed expression [, ...]] )
Copy after login

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.

The code is as follows:

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

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)
Copy after login

二var_export

(PHP 4 >= 4.2.0, PHP 5) 
var_export -- 输出或返回一个变量的字符串表示 
描述 
mixed var_export ( mixed expression [, bool return] )
Copy after login

This function returns the information about the transfer Gives the structure information of the variables of this 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'))) 这种与VAR_DUMP没什么区别; 
$var =var_export(array('a','b',array('aa','bb','cc')),TRUE),加上TRUE后,不会再打印出来,而是给了一个变量,这样就可以直接输出;
echo $var;此时输出来的形式与var_dump()打印的相似。
Copy after login

EG2

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

The output format is as follows:

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

The following is supplementary information:

error_log(var_export(yblog_mspconfiginit("ratings"),true));
Copy after login

Cause of the problem

var_export must return legal PHP code. In other words, the code returned by var_export can be directly assigned to a variable 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,

error_log(var_export(yblog_mspconfiginit("ratings"),true));老是打印出yblog_mspconfiginit(“ratings”)的返回是NULL
Copy after login

led me to think that the connection to the DB could not be established, and I took 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, var_export returns The code 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

Example

$res = yblog_mspconfiginit("ratings"); 
var_dump($res); 
var_export($res);
Copy after login

Result:

The code is as follows:

resource(1) of type (yahoo_yblog)
Copy after login

NULL Another example:

The code is as follows:

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

Result:

resource(2) of type (stream) 
NULL
Copy after login

Related recommendations:

php var_export function example explanation

##var_export function summary and comparison with var_dump

var_export and var_dump usage introduction_PHP tutorial

The above is the detailed content of Analysis of the difference between var_export and var_dump in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!