百科狗-知识改变命运!
--

var_export() - php 变量处理函数

乐乐1年前 (2023-11-21)阅读数 15#技术干货
文章标签变量

var_export()

(PHP 4 >= 4.2.0, PHP 5, PHP 7)

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

描述

var_export(mixed $expression[,bool $return]): mixed

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

var_export() - php 变量处理函数

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

比较var_export()和var_dump().

Looks like since version 5.4.22 var_export uses the serialize_precision ini setting, rather than the precision one used for normal output of floating-point numbers.
As a consequence since version 5.4.22 for example var_export(1.1) will output 1.1000000000000001 (17 is default precision value) and not 1.1 as before. 
/**
 * var_export() with square brackets and indented 4 spaces.
 */

fails. Interestingly, var_dump() has some logic to avoid recursive references. So :

works (while being more ugly). Unlike var_export(), var_dump() has no option to return the string, so output buffering logic is required if you want to direct the output.
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. 
There is an even simpler way to have clean output from var_export and print_r in html pages: 
[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

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...
WORKAROUND for error "Nesting level too deep - recursive dependency":
ob_start();
var_dump($GLOBALS);
$dataDump = ob_get_clean();
echo $dataDump;
I didn't see this simple little item anywhere in the user notes. Maybe I'm blind!
Anyway, var_export and print_r both use spaces and carriage returns for formatting. Sent to an html page, most of the formatting is lost. This simple function prints a nicely formatted array to an html screen: 
I have been looking for the best method to store data in cache files.
First, I've identified two limitations of var_export verus serialize. It can't store internal references inside of an array and it can't store a nested object or an array containing objects before PHP 5.1.0.
However, I could deal with both of those so I created a benchmark. I used a single array containing from 10 to 150 indexes. I've generate the elements' values randomly using booleans, nulls, integers, floats, and some nested arrays (the nested arrays are smaller averaging 5 elements but created similarly). The largest percentage of elements are short strings around 10-15 characters. While there is a small number of long strings (around 500 characters).
Benchmarking returned these results for 1000 * [total time] / [iterations (4000 in this case)]
serialize 3.656, 3.575, 3.68, 3.933, mean of 3.71
include 7.099, 5.42, 5.185, 6.076, mean of 5.95
eval 5.514, 5.204, 5.011, 5.788, mean of 5.38
Meaning serialize is around 1 and a half times faster than var_export for a single large array. include and eval were consistently very close but eval was usually a few tenths faster (eval did better this particular set of trials than usual). An opcode cache like APC might make include faster, but otherwise serialize is the best choice.
(This replaces my note of 3-July-2009. The original version produced no output if a variable contained an empty array, or an array consisting only of empty arrays. For example, $bigarray['x'] = array(); Also, I have added a second version of the function.)
The output can be difficult to decipher when looking at an array with many levels and many elements on each level. For example:

will return:
$bigarray = array(
... (500 lines skipped) ...
     'mod' => 'charlie',
Whereas the routine below can be called with:

and it will return:
$bigarray = array()
... (500 lines skipped) ...
$bigarray['foo']['bar']['0']['somethingelse']['mod'] = 'charlie'
Here's the function:

will return:
$bigarray = array(
... (500 lines skipped) ...
     'mod' => 'charlie',
Whereas the routine below can be called with:

and it will return:
$bigarray['firstelement'] = 'something'
... (500 lines skipped) ...
$bigarray['foo']['bar']['0']['somethingelse']['mod'] = 'charlie'
Here's the function:

鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com

免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)

图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)