为什么 var_dump 显示文件名和行号?

Why does var_dump show filename and line number?

最近 PHP 中的 var_dump()(当前使用 5.6.23)在实际转储我的变量之前开始打印出文件名和行号。我不知道服务器上有任何重大变化,所以我想知道为什么会这样,而且在网络上或 PHP-文档 (var_dump())[=13 中也找不到任何内容=]

使用命令行时也会发生奇怪的行为:

 > php -r 'var_dump("lol");'
 Command line code:1:
 string(3) "lol"

虽然我只是习惯了 "string(3) "lol"" 正在打印。

这不是一个障碍,但打破了我的几个单元测试,我需要比较一些使用 var_dump() 打印的 API 的输出。 我一开始以为它可能与xdebug有关,但找不到任何似乎与此问题有关的指令。

感谢任何导致此问题的提示。

您已启用 xdebug。

One of the new features relates to one of the first things that I added in the original Xdebug: making the var_dump() output "pretty". Xdebug replaces PHP's standard var_dump() function with its own version, as long as the xdebug.overload_var_dump setting is not set to 0


Xdebug 2.3 enhances the overloading of var_dump() with the inclusion of the file name and line number where var_dump() is called at. This has been a long standing feature request.

这是我没有 xdebug 的输出;

>php -r "var_dump('lol')";
string(3) "lol"

https://derickrethans.nl/xdebug-2.3-overload-vardump.html

如果您不想要 var_dump() 产生的额外数据,您可以使用 var_export(),它将显示 stripped-down 输出。

这是一个测试用例:

$values = [
    0 => '',
    1 => 'foo',
    2 => null,
    3 => false,
    4 => true,
    5 => 0,
    6 => new stdClass
];

var_dump():

foreach ($values as $value) {
    echo var_dump($value) . PHP_EOL;
}

普通旧PHP的输出:

string(0) ""
string(3) "foo"
NULL
bool(false)
bool(true)
int(0)
object(stdClass)#1 (0) {
}

PHP XDEBUG 的输出:

/var/www/html/test.php:12:string '' (length=0)
/var/www/html/test.php:12:string 'foo' (length=3)
/var/www/html/test.php:12:null
/var/www/html/test.php:12:boolean false
/var/www/html/test.php:12:boolean true
/var/www/html/test.php:12:int 0
/var/www/html/test.php:12:
object(stdClass)[1]

var_export():

foreach ($values as $value) {
    echo var_export($value) . PHP_EOL;
}

PHP 的输出(普通或使用 XDEBUG)

''
'foo'
NULL
false
true
0
(object) array(
)