Debug PHP with Sublime Text.



This blog post is about making sublime text to debug PHP. To do this we need  Xdebug ,PHP extension to work together with Sublime Text. I think this is something cool because most of you work with sublime text as it is a simple and a professional text editor,but if you are a PHP guy you may have to leave it because of not providing debug capabilities.So let's insert debugging in to sublime.


Step 1 - Installing Xdebug and making it work

  • > Create PHP file with the <?php phpinfo(); ?> code in it and save it in the root directory of your web server.In my case I use XAMPP,so I will save it in C:\xampp\htdocs\xampp\anything.php.
  • > Then load that page from your browser http://localhost/xampp/anything.php and press Ctrl+A(Select the whole content) and Ctrl+C(Copy content).
  • > Go to Xdebug Download and select  custom installation instructions under releases and paste it in the text box and click "Analyse my phpinfo() output ".
  • > If "Xdebug Installed" field in summary says "Only as php extension" if you have already installed Xdebug. If it says "No" then follow the instructions and install it. 
  • > After installing,you can confirm it by running phpinfo().You will see that "with Xdebug v2.40 XXXXXXXXXX"




Step 2 - Install sublime text plugin

In Sublime text, go to Preferences -> Package Control and then type "Install Package". When the repositories are loaded, type in "Xdebug" and choose "Xdebug Client".



After installing you will be able to set breakpoints in your code by Right click document -> Xdebug -> Add/Remove breakpoints.
Although Xdebug is installed, it is not configured properly.So we need to do some changes in "php.ini" file of the web server.

Step 3 - Configure Xdebug

  • >Go to xampp -> php ->php.ini
  • >Scroll to the bottom,there you will find the Xdebug section.If not create it as follows.
  • >Only uncomment/add following lines. (Leave the "zend_extention", as it depends on your installation and it was given in step 1)

[XDebug]
zend_extension = C:\xampp\php\ext\php_xdebug-2.4.0rc2-5.5-vc11.dll
xdebug.remote_enable = 1
xdebug.remote_handler = "dbgp"
xdebug.remote_host = "127.0.0.1"



Step 4 - Install Xdebug helper addon in your browser


Sublime text knows when to listen to browser when a certain cookie is set. For sublime this cookie is "XDEBUG_SESSION =sublime.xdebug".
We have to set this cookie manually unless we use a helper addon in our browser.In my case I use chrome,and the addon can be downloaded from the following link,so we can disable and enable debugging whenever we want.

Download Xdebug Helper Here

After installed, go to your Chrome Preferences -> Extensions -> Xdebug Helper -> Options and configure the addon as follows.



Step 5 - How to Debug

  • >Set a Breakpoint/Breakpoints in the php code.(Break point must be on a line of php code)
  • >Then go to Tools -> Xdebug -> Start Debugging.
  • >Then sublime text layout will change like in the following image



  • >Open your browser and load the php page/project.You will note a  "Bug" icon in right hand corner of the url bar.
  • >Click on it and make it "Debug".



  • >Refresh the page and proceed the functionality of the web page.
  • >When you hit the breakpoint,the web page will be freezed or remain loading.
  • >Now go to sublime text,You can now see the values/parameters passed and other related details.



  • >To move to the other breakpoint or to run the program , right click on the document -> Xdebug ->Run.




We are done.. It's debugging time..Try it! Comment if you need more or less..







0 comments: