当前位置: 首页 > 文档资料 > xdebug 中文文档 >

Variable Display Features

优质
小牛编辑
119浏览
2023-12-01

Xdebug replaces PHP's var_dump() function for displaying variables. Xdebug's version includes different colors for different types and places limits on the amount of array elements/object properties, maximum depth and string lengths. There are a few other functions dealing with variable display as well.

Effect of settings on var_dump()

There is a number of settings that control the output of Xdebug's modified var_dump() function: xdebug.var_display_max_children, xdebug.var_display_max_data and xdebug.var_display_max_depth. The effect of these three settings is best shown with an example. The script below is run four time, each time with different settings. You can use the tabs to see the difference.

The script

<?php
class test {
    public $pub = false;
    private $priv = true;
    protected $prot = 42;
}
$t = new test;
$t->pub = $t;
$data = array(
    'one' => 'a somewhat long string!',
    'two' => array(
        'two.one' => array(
            'two.one.zero' => 210,
            'two.one.one' => array(
                'two.one.one.zero' => 3.141592564,
                'two.one.one.one'  => 2.7,
            ),
        ),
    ),
    'three' => $t,
    'four' => range(0, 5),
);
var_dump( $data );
?>

The results

array
  'one' => string 'a somewhat long string!' (length=23)
  'two' =>
    array
      'two.one' =>
        array
          'two.one.zero' => int 210
          'two.one.one' =>
            array
              ...
  'three' =>
    object(test)[1]
      public 'pub' =>
        &object(test)[1]
      private 'priv' => boolean true
      protected 'prot' => int 42
  'four' =>
    array
      0 => int 0
      1 => int 1
      2 => int 2
      3 => int 3
      4 => int 4
      5 => int 5
array
  'one' => string 'a somewhat long string!' (length=23)
  'two' =>
    array
      'two.one' =>
        array
          'two.one.zero' => int 210
          'two.one.one' =>
            array
              ...
  more elements...
array
  'one' => string 'a somewhat long '... (length=23)
  'two' =>
    array
      'two.one' =>
        array
          'two.one.zero' => int 210
          'two.one.one' =>
            array
              ...
  'three' =>
    object(test)[1]
      public 'pub' =>
        &object(test)[1]
      private 'priv' => boolean true
      protected 'prot' => int 42
  'four' =>
    array
      0 => int 0
      1 => int 1
      2 => int 2
      3 => int 3
      4 => int 4
      5 => int 5
array
  'one' => string 'a somewhat long string!' (length=23)
  'two' =>
    array
      'two.one' =>
        array
          ...
  'three' =>
    object(test)[1]
      public 'pub' =>
        &object(test)[1]
      private 'priv' => boolean true
      protected 'prot' => int 42
  'four' =>
    array
      0 => int 0
      1 => int 1
      2 => int 2
      3 => int 3
      4 => int 4
      5 => int 5
array
  'one' => string 'a somewh'... (length=23)
  'two' =>
    array
      ...
  'three' =>
    object(test)[1]
      ...
  more elements...
'

Related Settings and Functions

Settings


integer xdebug.cli_color = 0 #

Introduced in Xdebug >= 2.2

If this setting is 1, Xdebug will color var_dumps and stack traces output when in CLI mode and when the output is a tty. On Windows, the ANSICON tool needs to be installed.

If the setting is 2, then Xdebug will always color var_dumps and stack trace, no matter whether it's connected to a tty or whether ANSICON is installed. In this case, you might end up seeing escape codes.

See this article for some more information.


integer xdebug.overload_var_dump = 2 #

Introduced in Xdebug > 2.1

By default Xdebug overloads var_dump() with its own improved version for displaying variables when the html_errors php.ini setting is set to true. In case you do not want that, you can set this setting to 0, but check first if it's not smarter to turn off html_errors.

New in Xdebug 2.3: You can also use 2 as value for this setting. Besides formatting the var_dump() output nicely, it will also add filename and line number to the output. The xdebug.file_link_format setting is also respected.

Before Xdebug 2.4: the default value of this setting was 1.


integer xdebug.var_display_max_children = 128 #

Controls the amount of array children and object's properties are shown when variables are displayed with either xdebug_var_dump(), xdebug.show_local_vars or through Function Trace.

To disable any limitation, use -1 as value.

This setting does not have any influence on the number of children that is send to the client through the Step Debugging feature.


integer xdebug.var_display_max_data = 512 #

Controls the maximum string length that is shown when variables are displayed with either xdebug_var_dump(), xdebug.show_local_vars or through Function Trace.

To disable any limitation, use -1 as value.

This setting does not have any influence on the number of children that is send to the client through the Step Debugging feature.


integer xdebug.var_display_max_depth = 3 #

Controls how many nested levels of array elements and object properties are when variables are displayed with either xdebug_var_dump(), xdebug.show_local_vars or through Function Trace.

The maximum value you can select is 1023. You can also use -1 as value to select this maximum number.

This setting does not have any influence on the number of children that is send to the client through the Step Debugging feature.

Functions


var_dump( mixed ...$var ) : void #

Displays detailed information about a variable

This function is overloaded by Xdebug, see the description for xdebug_var_dump().


xdebug_debug_zval( string ...$varname ) : void #

Displays information about a variable

This function displays structured information about one or more variables that includes its type, value and refcount information. Arrays are explored recursively with values. This function is implemented differently from PHP's debug_zval_dump() function in order to work around the problems that that function has because the variable itself is actually passed to the function. Xdebug's version is better as it uses the variable name to lookup the variable in the internal symbol table and accesses all the properties directly without having to deal with actually passing a variable to a function. The result is that the information that this function returns is much more accurate than PHP's own function for showing zval information.

Support for anything but simple variable names (such as "a[2]" below) is supported since Xdebug 2.3.

Example:

<?php
    $a = array(1, 2, 3);
    $b =& $a;
    $c =& $a[2];

    xdebug_debug_zval('a');
    xdebug_debug_zval("a[2]");
?>

Returns:

a: (refcount=2, is_ref=1)=array (
	0 => (refcount=1, is_ref=0)=1, 
	1 => (refcount=1, is_ref=0)=2, 
	2 => (refcount=2, is_ref=1)=3)
a[2]: (refcount=2, is_ref=1)=3

xdebug_debug_zval_stdout( string ...$varname ) : void #

Returns information about variables to stdout

This function displays structured information about one or more variables that includes its type, value and refcount information. Arrays are explored recursively with values. The difference with xdebug_debug_zval() is that the information is not displayed through a web server API layer, but directly shown on stdout (so that when you run it with Apache in single process mode it ends up on the console).

Example:

<?php
    $a = array(1, 2, 3);
    $b =& $a;
    $c =& $a[2];

    xdebug_debug_zval_stdout('a');

Returns:

a: (refcount=2, is_ref=1)=array (
	0 => (refcount=1, is_ref=0)=1, 
	1 => (refcount=1, is_ref=0)=2, 
	2 => (refcount=2, is_ref=1)=3)

xdebug_var_dump( mixed ...$variable ) : void #

Displays detailed information about a variable

This function displays structured information about one or more expressions that includes its type and value. Arrays are explored recursively with values. See the introduction of Variable Display Features on which php.ini settings affect this function.

Example:

<?php
ini_set('xdebug.var_display_max_children', 3 );
$c = new stdClass;
$c->foo = 'bar';
$c->file = fopen( '/etc/passwd', 'r' );
var_dump(
    array(
        array(TRUE, 2, 3.14, 'foo'),
        'object' => $c
    )
);
?>

Returns:

array
  0 => 
    array
      0 => boolean true
      1 => int 2
      2 => float 3.14
      more elements...
  'object' => 
    object(stdClass)[1]
      public 'foo' => string 'bar' (length=3)
      public 'file' => resource(3, stream)