Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does var_dump show filename and line number?

Tags:

php

var-dump

Just recently var_dump() in PHP (currently using 5.6.23) started to print out the filename as well as the line number before actually dumping my variable. I'm not aware of any major changes on the server, so I was wondering why this happens, also there's nothing to be found on the web or in the PHP-documentation (var_dump())

The strange behaviour also happens when using the command line:

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

While I'm just used to "string(3) "lol"" being printed.

This is not a showstopper but broke a couple of my unit-tests where I needed to compare some output from an API which is printed using var_dump(). I first thought it could be related to xdebug, but couldn't find any directive that seemd to be related to this problem.

Any hint what is causing this is appreciated.

like image 232
Select0r Avatar asked Jul 14 '16 13:07

Select0r


People also ask

What will be the output when Var_dump ($ States is written?

var_dump can't return any value it can only dump/print the values where as print_r can return the variable information if we set second parameter of print_r to true. The returned value of print_r will be in string format.

What is Var_dump () function explain with example?

The var_dump() function is used to dump information about a variable. This function displays structured information such as type and value of the given variable. Arrays and objects are explored recursively with values indented to show structure. This function is also effective with expressions.

What value will Var_dump show that Echo will not show?

The var-dump is display the datatype as well as value while echo only display the value. Explanation: In Php the var-dump function is used for displaying the datatype as well the value . The var-dump () does not return anythings it means there is no datatype of var-dump() function.

Why Var_dump is preferable over Print_r ()?

Why Var_dump () is preferable over Print_r ()? It's too simple. The var_dump() function displays structured information about variables/expressions including its type and value. Whereas The print_r() displays information about a variable in a way that's readable by humans.


2 Answers

You have xdebug enabled.

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.

Here is my output without xdebug;

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

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

like image 128
Maksym Semenykhin Avatar answered Sep 19 '22 08:09

Maksym Semenykhin


If you don't want the extra data produced by var_dump(), you can use var_export() which will show a stripped-down output.

Here is a test case:

$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;
}

Output of plain old PHP:

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

Output of 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;
}

Output of PHP (plain or with XDEBUG)

''
'foo'
NULL
false
true
0
(object) array(
)
like image 22
pbarney Avatar answered Sep 19 '22 08:09

pbarney