Tweet about this on TwitterShare on FacebookShare on Google+Share on StumbleUponShare on TumblrShare on RedditPin on PinterestEmail this to someone

This is not the first time I tried to start learning PHP and this is not the first time I wanted to have proper debugging environment for PHP as well. However every time I tried, I find more and more confusing to set up debugging properly. You can use netbeans, eclipse or any other paid IDEs for this, but it works sometimes and then stopped working, then again after few tweaks you can fix it to work but not sure for how long. Moreover I don’t see proper guide on how to setup PHP debugging, or different ways depending on web servers, IDEs. One reason may be that PHP is used by experienced programmers, they may not need fancy tools, debuggers, they can code on simple text editors, if they need they can use var_dump() or print_r() to see any debugging info. Other group who are not experienced PHP programmers and they may simply use pre-built frameworks such as Joomla, Drupal, WordPress etc  to setup their sites so they never want to dig into debugging. Developers like me who came from different programming background always want to learn other languages, do some complex coding but find it difficult in finding proper IDEs (either because they are high in price or free versions don’t support minimum features like debugging). That’s why this guide on How to Debug PHP , an easy approach.

What are the Options?

So what’s the answer, well I did the research for you, I tried eclipse (PDT) – I hate it, too bulky need java runtime (oh another thing I hate) even after that I find it difficult to setup debugging, even it supports you have to append query string XDEBUG parameters in order to make it work. Netbeans same story. Komodo Edit is very good IDE but doesn’t support debugging in its free version. Finally I found CodeLobster and its free version supports debugging and no need to append any query string parameters. Ok let me tell you this, you may not like the CodeLobster IDE for coding and you may already have preferred code editor for that, then of course you can use your editor for coding but once you find the need of debugging, switch to CodeLobster and debug, fix the issue and then switch back to your editor, continue work on that until you find next bug to debug :)

I was able to setup CodeLobster debugging in most complex development configuration I have. I’m using NGINX not Apache, my Virtual Host folders are way outside of usual default web root (www or htdocs). I’m using Laravel project with clean url format, (not simple php file to show you how to setup debugging). So after few hours of effort I was able to debug my Laravel project without any issue, and I’m going to show you exact same steps on how to do this.

Setting up the Environment

In my case I’m working on Windows 8, and I have installed WPИ-XM Web stack, but this is similar to any other web development stack such as WAMP (and I believe the steps are same for XAMPP as well) However you may not be able to use CodeLobster in any other OS. First thing you have to see and keep note of what is the PHP version installed with this, and whether the thread safety is supported or not. In any of the above web development installation you can check the phpinfo().  If not you can simply check that by creating a simple php file as follows and browse it or by command line php executable.

PHP Version and Thread safety

PHP Version and Thread safety

command line

Now we know that the version of PHP installed is 5.4.27 and does not support thread safety, this is important when selecting proper libraries for debugging setup which we will discuss next.
Click here if you don’t want spending time reading how setup Laravel sample web app and directly to see how to get debugging done

Setting up your PHP Web Application and Virtual Host (Optional)

This is an optional step as you may already have web application that you’re working on and it’s running well on your local development environment. However in order to illustrate complete process I will be creating simple Laravel web application and show you how to setup debugging based on that. You may need to use paths, files according to your setup.

Now you should have ‘laravel’ folder inside the D:\Temp folder. In Laravel entry point of the web application is ‘public’ folder. According to above installation, complete path is d:\Temp\laravel\public.

Ok we have laravel installed properly and then will create entry in nginx config file to point to this. If you’re using Apache web stack then configure it accordingly. (This is about how to configure PHP debugging not about setting up web development environment. So I hope you can handle that part your own)

As you can see I have point root element in the configuration file to public folder of new Laravel installation. I have also given the web address to my site ‘laravel.dev‘. Next thing is to open c:\windows\system32\drives\etc\hosts file and map the localhost to 'laravel.dev' domain name as below.

Now open the browser and go to ‘http://laravel.dev‘ and you should see Laravel home page.

I have written following route in route.php file just to illustrate the debugging when we setup it properly.

Download and Install CodeLobster

CodeLobster has different editions and the good thing is even its free version provides debugging feature. In addition to the it uses their own compilation of debugging library so we don’t need to use xdebug or query string parameters in order to debug the application. You can download CodeLobster from their site http://www.codelobster.com. Once you install you may need to get free registration key (via site or by application itself).

Next thing is to set some settings in CodeLobster and for that you may need the information we gathered before. Fire up CodeLobster and then go to Tools -> Preferences.. menu. Then expand the Debugger -> Settings from left tree view.

Virtual folder This is the entry point of our web application. In above Laravel installation this is D:\temp\laravel\public
Virtual Host Url This is the url for our web site. http://laravel.dev
Path to php ini If you remember we collected this value initially, in my case it is D:\WPNXM\bin\php\ ( only up to directory)
Web server In my case it couldn’t identify the web server, but it doesn’t matter leave it empty
Php version We have PHP 5.4.27 and thread safety is disabled, so select the correct one, PHP_5.4.x_VC9_Non_Thread_safe
CodeLobster Debug Settings

CodeLobster Debug Settings

Now click on Install Debugger. This should add entry to your php.ini file. This should also copy “php_cl_dbg_5_4_VC9_nts.dll” file into ext directory of the PHP installation folder. In my case the path is D:\WPNXM\bin\php\ext\php_cl_dbg_5_4_VC9_nts.dllHowever by any chance it doesn’t copy you have to manually copy it. You can find relevant CodeLobster debugging libraries inside CodeLobster installed folder. As in following image you will find different library for different PHP configuration. You must copy the correct file otherwise you will get different errors when you restart PHP and NGINX.

CodeLobster Debugging Libraries

CodeLobster Debugging Libraries

Next open the php.ini file and make sure new entry [CLDbg] is inserted at the bottom of the file. If you already have [Zend] entry and make sure to disable it as below. Also turn off xdebug.remote_enable option as well. After that make sure to restart your web server you should not get any error.

Create a CodeLobster Project

Ok now we need to create CodeLobster project for out Laravel web application. Go to Project -> Create Project… menu. Then enter path as in following image. This time we point to the laravel installation directory simply because we need to access all the files in order to debug. (Public folder only has single index.php file). You might or might not get a warning “The project directory is not within the virtual folder. Proceed?” anywya just proceed.

New CodeLobster Project

New CodeLobster Project

Now you can open route.php file (which we need to debug) and place a breakpoint next to first line as in below image. Then click on toolbar icon (Debug URL) or simply press F8 to start debugging. Once it prompted for URL type http://laravel.dev and click ok. This will launch the browser with given url.

Start Debugging

Start Debugging

Now we need to go to correct path as specified in above code, so type http://laravel.dev/test/333/debug/tutewall. This should now hit our break point. Yippeeeeeee!!. now you can step over each line of the code and view every variable. What a easy way to setup PHP debugging all with free tools but in professional manner.

Debugging view

Debugging view

 

Tweet about this on TwitterShare on FacebookShare on Google+Share on StumbleUponShare on TumblrShare on RedditPin on PinterestEmail this to someone