Skip to content

Instantly share code, notes, and snippets.

@paleo9
Last active September 11, 2017 23:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paleo9/47e8851b992f6615ef7cb2987e221d28 to your computer and use it in GitHub Desktop.
Save paleo9/47e8851b992f6615ef7cb2987e221d28 to your computer and use it in GitHub Desktop.
Using debuggers with PHP

PHP Debugging

At the time of writng I am using Debian Testing (stretch). The php-xdebug package has php7.0 as a dependency so it will be installed by default.

Installation

apt-get install php-xdebug

Using with Vim

The vdebug plugin requires python support in vim, but the version supplied by Debian only supports python3. So first step is to compile your own version of vim with python. I added to my .bashrc alias 'vimdgb=/path/to/my/vim/with/python'

  • install vdebug https://github.com/joonty/vdebug
  • press <f5> to start the debug session, it displays 'waiting for connection'
  • navigate to the page with a uri like http://localhost/myapp/index.php?XDEBUG_SESSION_START=dd1

vdebug commands

Run           <f5>
Step Over     <f2>
Step In       <f3>
Step Out      <f4>
Run to cursor <f9>
Detach        <f7> End debugging but let execution carry on as normal.
Stop          <f6> kill the program and close the debugger UI.

Set/remove Breakpoint <f10>
Return to context view after evaluating an expression: <f11> 
Evaluate variable under cursor: <f12>

vdebug breakpoints

vdebug has the following options for breakpoints:

  • line: stop at line. Set and remove with . Normal breakpoint.
  • conditional: :Breakpoint conditional . Break when this condition occurs.
  • exception: :Breakpoint exception . Break when this exception occurs.
  • call: :Breakpoint call . Break when function is called.
  • return: :Breakpoint return . Break when function returns.
  • watch: :Breakpoint watch . Break when value changes. Not supported in xdebug!

Examples

:Breakpoint conditional $x > 4
:Breakpoint exception PDOException
:Breakpoint call open_file
:Breakpoint call MyClass::OpenFile
:Breakpoint return open_file
:Breakpoint return MyClass::OpenFile
:Breakpoint watch $myvar

Set/remove line breakpoint <f10>

Viewing breakpoints

vdebug doesn't show breakpoints by default, but can open a window to show them. :BreakpointWindow To close, retype :BreakpointWindow or q

Tracing expressions

:VDebugTrace <expression>

example: :VDebugTrace $x

This opens the trace window which will show how any piece of code is evaluated as your code executes. The window stays oen until you close it manually.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment