pecl install xdebug
wget xdebug.org/link.php?url=xdebug201
tar -xzf xdebug-2.0.1.tgz
cd xdebug-2.0.1
phpize
./configure --enable-xdebug --with-php-config = / usr / bin / php-config
make
cp modules / xdebug.so /usr/lib/apache2/modules/xdebug.so
For Windows: zend_extension_ts = "c: \ php \ ext \ php_xdebug-2.0.1-5.2.1.dll"
For Unix: zend_extension = "/ usr / lib / apache2 / modules / xdebug.so"
xdebug.var_display_max_data =
in php.ini and restart your web server. Alternatively, you can change this setting in your script using the ini_set function, you can add
ini_set ('xdebug.var_display_max_data',);
at the beginning of your script. Make sure that the ini_set call is before the first var_dump () call. The xdebug configuration at runtime saves you from restarting the web server every time you change php.ini and allows you to configure it more flexibly.
You can also control the number of elements in the array and the properties of the objects that xdebug will output. This can be achieved by modifying xdebug.var_display_max_children (default value 128). This value will be quite enough to display the properties of your object, but if you work with arrays, it may be necessary to increase the value ...
If you work with nested objects or arrays, you can modify xdebug.var_display_max_depth. This setting has a default value of 3, which means that three dimensions are displayed in the array and three levels of nesting in the object.
You can also output the superglobal variable values using the xdebug_dump_superglobals () function. Since superglobal arrays, especially $ _SERVER, are arrays containing a large number of values, you must explicitly tell xdebug which key of the array you want to see. To do this, install xdebug.dump., Where it is one of the following names GET, POST, SERVER, COOKIE, FILES, REQUEST or SESSION. Use the keys of the arrays that you want to output with xdebug as arguments, if you want to display several values, then list them with a comma. You can use * as a template for displaying keys, which can be especially useful for outputting $ _GET and $ _POST.
Use
ini_set ('xdebug.dump.SERVER', 'HTTP_HOST, SERVER_NAME')
in your PHP script or xdebug.dump.SERVER = HTTP_HOST, SERVER_NAME settings in php.ini to display the values $ _SERVER ['HTTP_HOST'] and $ _SERVER ['SERVER_NAME']. To display all the GET values that were passed to the script, use
xdebug.dump.GET = *
![]()
By default, xdebug does not display undefined variables. To still display undefined variables, set the xdebug.dump_undefined parameter to On. I suggest setting this parameter to on.
More beautiful error messages
xdebug also improves the display of errors in PHP by automatically displaying a call stack next to each error message or warning. This call list displays the call history of functions until an error message appears. While PHP programs are becoming more and more object-oriented, errors most often occur deep inside libraries. The call list will allow you to quickly find the piece of code in which the error occurred, and from where this piece was called.
Since PHP 5.0, the debug_print_backtrace () function has appeared in PHP, which displays a list of function calls, but you cannot call this function explicitly whenever an error occurs, which means you need to create your own error handler. The call list created by xdebug is easy to read, in contrast to the output of PHP functions.
Like PHP, xdebug displays errors only when the display_erorrs parameter is set to On in php.ini.
![]()
Look at this call list, you will see that the function foo () was first called, then bar (), then baz (). In addition to the names of the called functions and the location in the source code, xdebug displays the time and amount of memory used by the script at the time of calling these functions.
Above, we have already learned how to configure xdebug to display the values of superglobal variables. In addition, you can configure xdebug to display the current values of local variables. In order to display the values of local variables when an error message occurs, add the following line
xdebug.show_local_vars = 1
in php.ini. You can also use ini_set.
![]()
So the php.in xdebug.var_display_max_data, debug.var_display_max_children and xdebug.var_display_max_depth parameters that we used above to format the output of the var_dump () command also affect the error output format.
Using the xdebug.collect_params parameter, you can customize the display of information about the parameters that are passed to the functions. xdebug.collect_params has a numeric value: 0 means no information, and 4 means displaying the names of variables and complete information on all parameters of the functions. Values from 1 to 3 are also available, meaning different granularities in the output of debug information.
![]()
Here is the configuration used to display the screenshot:
xdebug.show_local_vars = On
xdebug.dump.SERVER = HTTP_HOST, SERVER_NAME
xdebug.dump_globals = On
xdebug.collect_params = 4
Please note that the more information you ask xdebug to collect for you, the more memory it will consume, and the longer the execution time will be. Make sure you use xdebug for development purposes only, and not on combat servers. It is wrong to use error messages in HTML format on the combat servers, it is not good to output information on the login and password to the database if you have not defined the variable .
While superglobals do not change at runtime (the translator’s note is a strange statement, I saw quite a few scripts that changed them, but we will not discuss this in the context of this article), xdebug displays them by default only at the time of the first message about the error. If you want to repeat their output every time, use the following setting.
xdebug.dump_once = Off
I draw your attention to the fact that extended error reporting does not work if you define your own error handler using register_error_handler (), everything happens because xdebug uses the same internal mechanism. If you want to use your own error handler, you can use the xdebug_get_function_stack () function to display a function call list.
The object-oriented approach uses exceptions. Since exceptions are not errors, xdebug does not output a list of calls if an exception has occurred, except when such an exception is not intercepted. An uncaught exception is a fatal error. To see the function call when exceptions occur, usexdebug.show_exception_trace = On
Recursion limit
Another useful feature is the restriction of recursion steps, which prevents an endless sequence of recursive calls.
xdebug prevents endless recursion by stopping the script at a predefined step. In principle, this is a limit to the size of the call stack. The default value of this parameter is 100, and this feature is enabled by default. You can change the setting if your program means deeper recursion.
xdebug.max_nesting_level =
However, xdebug does not prevent infinite for, while, and similar loops, since they do not increase the call stack. In addition, you can use the counter inside each cycle and kill the script whenever the counter value exceeds the allowed number. I advise you to use this approach on combat servers, because there should not be endless cycles.
Conclusion
xdebug is a small but very useful PHP developer tool, it should be installed on every PHP installation used for development. You should not use xdebug on combat servers, as this interferes with performance.
The next article will be devoted to Tracing PHP Applications with xdebug and will be translated somewhere later this week.
Source: https://habr.com/ru/post/31452/