When I debug php with var_dump variable it always outputs file path at the beginning?

泄露秘密 提交于 2019-12-10 16:50:39

问题


I am using Ubuntu with PHP 7.

PHP 7.0.5-3+donate.sury.org~xenial+1 (cli) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies
    with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2016, by Zend Technologies
    with Xdebug v2.4.0, Copyright (c) 2002-2016, by Derick Rethans

When I debug a PHP script by using var_dump to show some variable:

<?php
var_dump('tmp string');
var_dump(true);

The below is its output:

/var/www/example.com/test.php:3:string 'tmp string' (length=10)
/var/www/example.com/test.php:4:boolean true

Why does it always output with the file path before?

I want it to output like below:

string 'tmp string' (length=10)
boolean true

回答1:


The output you're seeing is from the Xdebug extension. (Without the extension, var_dump outputs plain, unformatted text.)

From Xdebug 2.3, the setting xdebug.overload_var_dump has a new default value of 2 which adds the filename and line number to the output from any call to var_dump. See the docs for more info. I agree it's not that useful, especially for simple output like short strings/numbers.

To remove the filename you can set the option to the old value of 1 in your php.ini file:

[xdebug]
xdebug.overload_var_dump = 1



回答2:


I ended up here from a search on the topic, but I continued to look for other alternatives and just wanted to add what I found for others.

As of version, Xdebug >= 2.6, there are a few options to modify the display of the file name shown in a var_dump.

// real output example of the %n specifier
courses_list.php:14:string 'tools' (length=5)

Ref: https://xdebug.org/docs/all_settings#filename_format

xdebug.filename_format = "[Specifier]"

Type: string, Default value: ...%s%n, Introduced in Xdebug >= 2.6

This setting determines the format with which Xdebug renders filenames in HTML stack traces (default: ...%s%n) and location information through the overloaded xdebug_var_dump() (default: %f).

  • Specifier : %a

    • Meaning: Ancester: Two directory elements and filename
    • Example Output: mail/transport/mta.php
  • Specifier : %f

    • Meaning: Full path
    • Example: /var/www/vendor/mail/transport/mta.php
    • (is default setting)
  • Specifier: %n

    • Meaning: Name - Only the file name
    • Example: mta.php
  • Specifier: %p

    • Meaning Parent - One directory element and the filename
    • Example: transport/mta.php
  • Specifier: %s

    • Meaning: Directory separator
    • Example: \ on Linux, OSX and other Unix-like systems, / on Windows


来源:https://stackoverflow.com/questions/36939095/when-i-debug-php-with-var-dump-variable-it-always-outputs-file-path-at-the-begin

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!