Debugging PHP with xdebug and notepad++

Posted in PHP by

Debugging PHP helps fixing your bugs faster and removes debug code from your script forever. Stop using "echo" and "var_dump", instead use xdebug and notepad++ to debug your script, like one debugs a c++ program.

the setup You need notepad++ with DBGD Plugin. Download both yourself, or get directly. Grab the lastest version of the xdebug php extension, and put it in your "php/ext" directory. If you use easyphp, you need "VC9 TS (32 bit)".

install Open your php.ini (in case of easyphp, "conf_files/php.ini"). And add or update it, do not forget to adjust the name of php_xdebug-X.X.X-X.X-XXX.dll to the real name.

;Xdebug
zend_extension = "${path}\xdebug\php_xdebug-2.1.2-5.3-vc9.dll"
xdebug.default_enable=1
xdebug.remote_enable=1
xdebug.remote_host=127.0.0.1
xdebug.remote_port=9000
xdebug.remote_handler=dbgp
xdebug.remote_mode=req
xdebug.remote_autostart = 1
xdebug.dump_globals=1
xdebug.dump=COOKIE,FILES,GET,POST,REQUEST,SERVER,SESSION
xdebug.dump.SERVER=REMOTE_ADDR,REQUEST_METHOD,REQUEST_URI
xdebug.show_local_vars=1
xdebug.show_mem_delta=1
xdebug.collect_includes=1
xdebug.collect_vars=1
xdebug.collect_params=4
xdebug.collect_return=1
xdebug.auto_trace=0
xdebug.trace_options=0
xdebug.trace_format=0
xdebug.trace_output_dir="${path}\xdebug\trace"
xdebug.trace_output_name="trace.%t"
xdebug.profiler_enable=0
xdebug.profiler_append=1
xdebug.profiler_enable_trigger=1
xdebug.profiler_output_dir="${path}\xdebug\profiler"
xdebug.profiler_output_name="cachegrind.out.%s.%t"
;/Xdebug
You are ready to debug your first script now!

debugging Go to Notepad++, Menu, Plugins, DBGp, Debugger. Develop your scripts as usual.Set breakpoints using the red button. Switch between Local context and Global context (you might need to right-click and refresh, or just adjust the debugger setting to refresh local context at every step). There are several buttons left of the red button. The first one diggs as deep as possible, the second one stays within the current file or function and the third one trys to move out of the current function or class. The next button will run till the position of your curser, the run button executes till the next breakpoint is hit or the script ends. And the stop button, terminates the script.

benefits Using xdebug allows you to debug php scripts, which helps you to understand how php works, or why something does not work as expected. You no longer need to add "echo" or "print_r" statements, because you can just set a breakpoint and see the values of all current variables. And in the end, you will write better code in less time and fix your bugs without trial and error.

Feel free to comment about it, or ask questions in the comments.

Published at , Updated at 2011-10-25

next: Namespace prev: syntax, variables, comments and functions