Quick Guide to XDebug and Vim with MAMP

If you program in php and don’t debug you are probably all too familiar with dropping echos and var_dumps over your code to try and figure out what is going on. Not only is the ugly and unelegant but sometimes you forget about these statements and they creep back to haunt you later, XDebug will save you all of that hassle. XDebug allows you to step through your code, set breakpoints, and access variable states. After your first time using this you will probably lose your mind on how no one ever mentioned it to you before, I can’t believe no one told me!

Web Page Debugging

My setup is for a mac running MAMP 2.x and Vim but the instructions will reflect similarly to other situations. MAMP 2.x has XDebug installed but not enabled by default and MAMP 3 apparently has this functionality in a single button. To get things rolling open the php.ini file in MAMP for the version you are using and look towards the bottom for the following:


You should quickly confirm that file is actually there just to save yourself some potential headache. Uncommenting this line is all you need to get rolling but were going to add a couple of other options underneath as well:


The first 4 xdebug variables are copy paste from another blog but the autostart is an additional one to make your life easier. See if you don’t use autostart you will have to append “?XDEBUG_SESSION_START=1” to all of your request urls so I just add this because you probably don’t want to do that.. Make sure you restart the MAMP server after making these changes and confirm that xdebug modules are being loaded with phpinfo(). Now you need to setup Vim for XDebug. I use the vdebug plugin for vim and I would recommend it to everyone else, I don’t even know if there are any other real alternatives. Open Vim and press F5 to get the “waiting for connection message”. You then have 20 seconds to open the url in a browser that you wish to debug. If everything was successful then you should see your vim window light up on the first line of code executed by your url. Since I use Drupal mostly for my PHP programming I’ll set a breakpoint in my code (F10) and hit F5 one more time after the connection is made so the program executes up to that breakpoint. You can now see the window which contains all the variables used in the script as well as all the global variables. You can step through and into all your code and get full confirmation that everything is working as it is supposed to be.

Command Line Debugging

Now that you can debug your webpages you might be thinking of a way to debug php scripts of even more, debug unit tests. We it turns out it’s much easier than I expected, I blindly pasted code trying to figure this out when I realized that I had the answers all along. PHP command line executes from a separate php.ini as the web pages do. Once I opened up the second ini file by using:

php --ini

I simply copied and pasted the code from earlier php.ini into the the ini file for command line. I opened two terminals up, one with vim and one for launching the request. Opened vim and hit F5 while moving to the next terminal and typing:

php myscript.php

Which brought up the same screen in vim as the webpage did. Armed with this knowledge you can now debug either webpages or command line scripts and save yourself endless hours of echos, prints and var_dumps.