The document discusses using Xdebug to debug PHP code. It begins with an overview of Xdebug, explaining that it is a PHP extension that allows for step-by-step debugging and profiling of PHP applications. It then covers how to set up Xdebug, including installing the extension, configuring an IDE like PhpStorm to work with it, setting breakpoints in code, and activating the debugger. Finally, it discusses how to use Xdebug, such as setting and working with breakpoints, stepping through code, and using watches and the console window. The overall document serves as an introduction to debugging PHP applications with Xdebug.
4. A B O U T M A R K N I E B E R G A L L
• PHP since 2005
• Masters degree in MIS
• Senior Software Engineer
• Drug screening project
• UPHPU President
• CSSLP, SSCP Certified and SME
• Drones, fishing, skiing, father, husband
5. A B O U T M A R K N I E B E R G A L L
• Manchester
• Nelson
• Workington
14. W H Y U S E X D E B U G
Debugging without Xdebug
15. W H Y U S E X D E B U G
Debugging without Xdebug
• echo $something;
• var_dump($other);
• print_r($data);
• error_log(__FILE__ . ‘ line ‘ . __LINE__);
16. W H Y U S E X D E B U G
Debugging without Xdebug
1. Add outputs
2. Load page
3. Look for output
4. Change some code
5. Add more outputs
6. Repeat
7. Cleanup outputs without missing any
8. Load page
18. W H Y U S E X D E B U G
Debugging with Xdebug
1. Turn on debugging
2. Add breakpoint(s)
3. Load page
4. Step into the code
19. W H Y U S E X D E B U G
Debugging with Xdebug
• Live values
• Follow actual code path
• Full stacktrace
20. W H Y U S E X D E B U G
Debugging with Xdebug
• Supported by most IDEs
• Faster development time
• Better understanding of what code is actually doing
• Does not require code changes to interrogate values
22. S E T T I N G U P X D E B U G
Fire up you development environment
23. S E T T I N G U P X D E B U G
Documentation from JetBrain for PhpStorm
https://confluence.jetbrains.com/display/PhpStorm/Zero-
configuration+Web+Application+Debugging+with+Xdebug+and+PhpStorm
1. Install Xdebug
2. Prepare PhpStorm
3. Set a breakpoint in the source code
4. Activate debugger on server
5. Start a debug session in browser
6. Reload the current page
7. Set initial path mappings
24. S E T T I N G U P X D E B U G
1. Install Xdebug
25. S E T T I N G U P X D E B U G
1. Install Xdebug
• https://xdebug.org/docs/install
26. S E T T I N G U P X D E B U G
1. Install Xdebug
• Download (if applicable)
• Install or compile
• Configure PHP
• Restart webserver
27. S E T T I N G U P X D E B U G
1. Install Xdebug
• git clone https://github.com/xdebug/xdebug.git
• cd xdebug
• ./rebuild.sh
28. S E T T I N G U P X D E B U G
1. Install Xdebug
• Windows:
- Xdebug website has a wizard, dll downloads
• Mac:
- sudo pecl install xdebug
- brew install php72-xdebug
• Linux:
- wget http://xdebug.org/files/xdebug-2.6.0.tgz
- sudo pecl install xdebug
29. S E T T I N G U P X D E B U G
1. Install Xdebug
• Windows: download dll
- Xdebug website has a wizard, downloads
- Paste php -i or phpinfo() (ex: C:phpphp -i > C:
phpphpinfo.txt or <?php phpinfo();)
- dll download link provided
- Add a line to your php.ini file
- Restart webserver
30. S E T T I N G U P X D E B U G
1. Install Xdebug
• apt-get install php-xdebug
• wget http://xdebug.org/files/xdebug-2.6.0.tgz
tar -xzvf xdebug-2.6.0.tgz
cd xdebug-2.6.0
phpize
./configure --enable-xdebug
make
make install
31. S E T T I N G U P X D E B U G
1. Install Xdebug
• yum install php-devel
yum install php-pear
pecl install xdebug
• dnf install php-devel
dnf install php-pear
pecl install xdebug
32. S E T T I N G U P X D E B U G
1. Install Xdebug
• Update php.ini by adding lines:
zend_extension = php_xdebug-2.6.0-7.2-vc15-nts-x86_64.dll
[xdebug]
xdebug.remote_enable = 1
xdebug.remote_autostart = 1
xdebug.remote_port = 9000
xdebug.remote_host = localhost
xdebug.idekey = PHPSTORM
33. S E T T I N G U P X D E B U G
1. Install Xdebug
• Update php.ini by adding lines:
zend_extension = /usr/lib64/php/modules/xdebug.so
[xdebug]
xdebug.remote_enable = 1
xdebug.remote_autostart = 1
xdebug.remote_port = 9000
xdebug.remote_host = localhost
xdebug.idekey = PHPSTORM
34. S E T T I N G U P X D E B U G
1. Install Xdebug
• Restart webserver
- Windows:
Services
Apache Monitor
- Linux:
sudo apachectl restart
sudo service nginx restart
35. S E T T I N G U P X D E B U G
1. Install Xdebug
• Verify via phpinfo
- <?php phpinfo();
- php -i | grep xdebug
36. S E T T I N G U P X D E B U G
1. Install Xdebug
• Verify via phpinfo
37. S E T T I N G U P X D E B U G
1. Install Xdebug
• Verify via phpinfo
38. S E T T I N G U P X D E B U G
2. Prepare PhpStorm
39. S E T T I N G U P X D E B U G
2. Prepare PhpStorm
• Start Listening for PHP Debug Connections
40. S E T T I N G U P X D E B U G
2. Prepare PhpStorm
41. S E T T I N G U P X D E B U G
2. Prepare PhpStorm
42. S E T T I N G U P X D E B U G
2. Prepare PhpStorm
• Setup debug settings
43.
44. S E T T I N G U P X D E B U G
2. Prepare PhpStorm
• Setup project path mapping
45. S E T T I N G U P X D E B U G
2. Prepare PhpStorm
46. S E T T I N G U P X D E B U G
3. Set a breakpoint in the source code
47. S E T T I N G U P X D E B U G
3. Set a breakpoint in the source code
• Line you know will be hit
• Or break on first line
48.
49. S E T T I N G U P X D E B U G
4. Activate debugger on server
50. S E T T I N G U P X D E B U G
4. Activate debugger on server
• Browser plugin, add-on
- The easiest Xdebug (Firefox)
- Xdebug Helper (Chrome)
- Xdebug Toggler (Safari)
- Xdebug launcher (Opera)
51. S E T T I N G U P X D E B U G
4. Activate debugger on server
52. S E T T I N G U P X D E B U G
4. Activate debugger on server
53. S E T T I N G U P X D E B U G
4. Activate debugger on server
• URL
- GET: &XDEBUG_SESSION_START=PHPSTORM
- POST: XDEBUG_SESSION_START=PHPSTORM
• Headers
- Name: Cookie
- Value: XDEBUG_SESSION=PHPSTORM
• Bookmarklet
54. S E T T I N G U P X D E B U G
4. Activate debugger on server
55. S E T T I N G U P X D E B U G
4. Activate debugger on server
56. S E T T I N G U P X D E B U G
4. Activate debugger on server
57. S E T T I N G U P X D E B U G
4. Activate debugger on server
58. S E T T I N G U P X D E B U G
4. Activate debugger on server
59. S E T T I N G U P X D E B U G
4. Activate debugger on server
60. S E T T I N G U P X D E B U G
4. Activate debugger on server
61. S E T T I N G U P X D E B U G
5. Start a debug session in browser
62. S E T T I N G U P X D E B U G
5. Start a debug session in browser
• Click the debug button in toolbar
• Add GET, POST, or COOKIE
63. S E T T I N G U P X D E B U G
6. Reload the current page
64. S E T T I N G U P X D E B U G
6. Reload the current page
65. S E T T I N G U P X D E B U G
7. Set initial path mappings
66. S E T T I N G U P X D E B U G
7. Set initial path mappings
71. U S I N G X D E B U G
Breakpoints
• Pause code execution at specific line
• Allowed multiple breakpoints
• Conditional breakpoints
72. U S I N G X D E B U G
Breakpoints
• Place strategically
• Not too early
• Not too late
• Not too many
• Remember time limit, increase if needed
• Use conditional breakpoints in loops
76. U S I N G X D E B U G
Stepping through code
• Resume Program (Play)
• Stop
• View Breakpoints
• Disable All Breakpoints
77. U S I N G X D E B U G
Stepping through code
• Step Over
• Step Into
• Force Step Into
• Step Out
• Run to Cursor
78. U S I N G X D E B U G
Stepping through code
• Evaluate Expression
• Show value addresses
• Hide empty superglobal variables (on by default)
• Add method to skip list
84. S U M M A RY
• More efficient way to debug
• Visibility into code
• Ability to change code on the fly
• Watch values change line by line
• Better understanding of code path
85. D I S C U S S I O N
• Setup issues
• Remote debugging
• Profiling
• Xdebug vs alternative methods
86. Q U E S T I O N S ?
https://joind.in/talk/50bee
87. S O U R C E S
• JetBrains documentation https://confluence.jetbrains.com/display/PhpStorm/Zero-
configuration+Web+Application+Debugging+with+Xdebug+and+PhpStorm
• Xdebug documentation https://xdebug.org/docs/install
• David Stockton, php[architect], January 2015, https://www.phparch.com/wp-
content/uploads/2015/01/levelup-xdebug-phparchitect-jan2015.pdf
• https://sievertschreiber.files.wordpress.com/2010/04/crane-truck-double-fail.jpg