Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

var-dump

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
like image 286
Shi Falei Avatar asked Jan 07 '23 04:01

Shi Falei


2 Answers

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
like image 197
DisgruntledGoat Avatar answered Jan 08 '23 18:01

DisgruntledGoat


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
like image 26
Terre Porter Avatar answered Jan 08 '23 18:01

Terre Porter