Interactive Debugging in PHP

What we're doing now?

var_dump($var);

print '<pre>' .print_r($var, true) . '</pre>';

var_dump(debug_backtrace());

dump($var); die();

Any more cool know-hows?

What is debugging?

  • Finding broken code.
  • Figure out why.
  • Fix.
  • Check.
  • Repeat.

How can we improve?

  • Use Symfony VarDumper
  • Use Xdebug's dump
  • Use Firebug with FirePHP for anyc requets

An even better option!

Interactive debugging:

  • ZendDebugger
  • XDebug

Understanding HTTP Requests

GET / HTTP/1.1

Host: example.com
Cookie: XDEBUG_SESSION=PHPSTORM

Xdebug:

Zend Debugger:

GET / HTTP/1.1

Host: example.com

Cookie: debug_host=192.168.50.1; debug_port=10137; debug_start_session=1

Query params are also possible: 

GET /?debug_host=192.168.50.1&debug_port=10137 HTTP/1.1

Host: example.com

Server Environment

printenv

vagrant@fusion:~$ printenv
SSH_AGENT_PID=14079
XDG_SESSION_ID=3
TERM=xterm
SHELL=/bin/bash

php -i 

PHP Variables
Variable => Value
_SERVER["SSH_AGENT_PID"] => 14079
_SERVER["XDG_SESSION_ID"] => 3
_SERVER["TERM"] => xterm
_SERVER["SHELL"] => /bin/bash
_SERVER["argv"] => Array()
_SERVER["argc"] => 0
_SERVER["PHP_SELF"] =>
_SERVER["DOCUMENT_ROOT"] =>

So... How what do you do next?

  1. Setup a debugger
  2. Install and IDE that supports debugging 
  3. Either set the cookie or query params
  4. Send the request
  5. ???
  6. Profit!

Setting up Xdebug

  1. Connect to your Vagrant box
  2. sudo vi /etc/php5/mods-available/xdebug.ini
  3. Add a remote host: 192.168.50.1
  4. :wq
  5. sudo service php5-fpm restart

Your xdebug.ini should look like this:

Debugging with phpStorm

  1. Create a debug bookmarklet
  2. Listen for Debug connections
  3. Set a Cookie in the browser
  4. Send the request
  5. Open phpStorm and respond to an incoming connection

Run > Start Listen for PHP Debug Connections

2.

Map the root of the project

In this case we map to: /home/vagrant/Code/src/fusion-api-epg

phpdebug

By Alex Niedre

phpdebug

Debugging in PHP

  • 633