We available for hire! — Need help with a web or mobile project?
From yensdesign we develop technology projects based on web apps, mobile apps, consulting and databases. We are young and hard workers, passionate about innovation and focused in new technologies.If you want to ask for a budget, we hare available for hire!.
How to debug PHP code
October 7th, 2008Nowadays, the code debugger is a tool that has almost all programing enviroments which allows you to watch the internal work of your applications and finding errors at run time for a easy way. The trouble is when you are working in a client-server architecture model, because from where you send the request (client) can not access the code hosted on the server.
To PHP exists any debuggers that require an installation in the server part, that can not always like you. However you can find useful tools that can help you to make this task without to be debugger exactly (without interrupted point or execution step by step…).
One of this tools is FirePHP:
FirePHP is a Mozilla Firefox complement that merges with Firebug and allows you to watch the outputs sent from PHP code in the console, so, you can watch the variables values or create a log about that is happened in your scripts without use the upset “echo” that interferes with you HTML.
The way FirePHP works is very easy, all the information that you want to view at run time is coded in JSON and sent in the headers of the response. When these headers arrives to the navigator, FirePHP detect it and to render the information in the Firebug console.
So relax, take a cup of tea and let’s begin this lesson.
Step 1: What do you need to install?
- Mozilla Firefox
- Firebug
- FirePHP
- FirePHP Core (a PHP code class to send the headers
Step 2: Here you have a little example
If you have installed all requisites, you are ready to make the classic “Hello world!” example. In your web server create a new directory in the publication directory. In this directory create a new php file called index.php and copy the file firephp.class inside too. Write the bellow in index.php:
[sourcecode='PHP']
//include the file
require_once("firephp.class");
//create the object
$firephp = FirePHP::getInstance(true);
//send information
$firephp->fb(“Hello world!”);
?>
[/sourcecode]
To run this code you can see the message “Hello world!” in the firebug console:
The syntax of the fb method is very easy:
fb (object, [type])
The first param is the object(variable) you want to watch and the second is an optional parameter which indicates how the content is going to be shown. FirePHP can find the the type automatically, however if you want to specify it, these are the availables values:
- FirePHP::LOG
- FirePHP::INFO
- FirePHP::WARN
- FirePHP::ERROR
- FirePHP::DUMP
- FirePHP::TRACE
- FirePHP::EXCEPTION
- FirePHP::TABLE
For you see it with some functionality:
[sourcecode='PHP'] name = "Name"; $o->desc = "This is my name"; $arr = array("orange", "yellow","blue"); try { $firephp->fb("Starting..."); $firephp->fb($o); $firephp->fb($arr,FirePHP::TRACE); //Force an exception throw new Exception("The value is not correct!"); } catch(Exception $e) { //$e will be render as FirePHP::EXCEPTION automatically $firephp->fb($e); } $firephp->fb("End!"); ?> [/sourcecode]
Output in the console:
Step 3: How to release?
When you put your files in the production server you must delete all calls to fb function, for several reasons. The first reason is for security, somebody that has installed the FirePHP extension and enter into your website, may watch your debug log (that must not be very good… ). The second reason is for performance, it is not necessary to send the headers into your responses to the client if these will be not used. How to solve this? Surely, the first idea that appears in your mind is declare a global variable that indicates if the program is in debug mode or not, then include all calls to function fb in this conditional:
[sourcecode='PHP'] if(DEBUG) $firephp->fb("Debugging..."); [/sourcecode]
Could be a solution… However you are doing PHP to evaluate conditionals that don’t have useful to the client. What is my solution? I chose to include all calls to debug functions between the tags #DEBUG and #DEBUG_END as below:
[sourcecode='PHP'] fb($ob); #DEBUG_END ?> [/sourcecode]
What is that?, is there really so? No, it is small tool that I have created to define parts of code which only are required in debug mode. I have created a little program (you can download PHPReleaser here) that iterates recursively with all PHP files in the input directory deleting all code included between these tags, and save them in the specific output directory.
Once this is done, you can upload the files to your web server with the tranquility that your script will not execute more lines of code needed. In addition, you should also use a code compressor that delete all spaces, line breaks and comments for a better performance.
Note: The phpreleaser that is attached, is only a sample version, you can extend it or translate it to other programming language. I would like if you do it, send it to me and I will upload.
Whats your opinion about it? Good, bad, has troubles… Waiting for your comments! See you in the next tutorial!
One more thing…
The certkiller offers you 100% success on 642-436 exam in first attempt. Just download the 640-816 dumps to practice and walk in exam with confidence. We also offer superb HP0-D07 training for guaranteed exam preparation.
Enjoy this post?
Your vote will help us to grow this website and write more entries like this one :)
We available for hire! — Need help with a web or mobile project?
From yensdesign we develop technology projects based on web apps, mobile apps, consulting and databases. We are young and hard workers, passionate about innovation and focused in new technologies. We try to help you, sharing our knowledge acquired on worked projects, while helping the community and showing our capabilities.If you want to ask for a budget, we hare available for hire! Don't doubt in get in touch with us!.
Thanks for the tip.
Some FirePHP links are wrong, they point to FireBug’s page at Mozilla, you might want to fix them.
Also on the DEBUG stuff, you probably won’t notice a difference between using a conditional or removing them completely. Did you measure it with a profiler?
hi Loïc Hoguin, thanks for your comment! I have updated the links, you have reason, the links should point to the project home page.
I have not used any profiler, however, the debug code shouldn’t stay in the final product. When you compile a binary software in win32 (for example) you can choose debug mode or release, if you choose release, all code relative to debugging will not compile, why not do it in php? In addition, i say to you other thing, sometimes in the programs, the miliseconds are gold
[...] Guardado has posted a recent tutorial looking at debugging your PHP code with the help of a popular tool – FirePHP (that interfaces with [...]
The only place I see where this is an improvement over littering your code with echo or print statements is that the average user will not see them. It’s still an in-efficient and in-exact way of dealing with debugging. You need to modify and re-upload code if you discover that your debugging outputs are not giving you detailed enough output to determine the problem.
Your best bet is to have a development server that has XDebug or Zend-Debugger installed and a debugger client or IDE that can speak to them. This gives you access to a true debugger, and the ability to do things such as step through your code line by line, monitoring variables as you go. It’s easy enough to do this on any computer. For windows you can download many all-in one development stacks like XAMPP or WAMPServer. Grab the debugger binary of your choice and install it. Doing this on Linux is even easier as most distributions have all the components of a LAMP server in their repositories, and both XDebug and Zend are available for Windows or Linux. On top of that the Eclipse PDT project is able to work with either debugger and itself is a cross platform IDE.
Just my $0.02
I have not said that this is the perfect solution. Obviusly if you want to debug with all funcionalities you must use Zend-Debugger or XDebug, but firephp is a well and easy way. As I have said:
“[..]exists any debuggers require an installation in the server part, that can not always like you[..]“.
This is just another option for PHP debugging.
Thanks for your opinion JGM!
How to debug PHP code…
[...]Nowadays, the code debugger is a tool that has almost all programing enviroments which allows you to watch the internal work of your applications and finding errors at run time for a easy way. The trouble is when you are working in a client-server…
conditional access…
Maybe, but I’m not sure it’for everyone….
Nice tutorial, I’ve used firebug for a while now but I never knew about firePHP. Thanks and keep up the great work.
[...] Kako debugirati PHP kodo – firePHP [...]
Very informative article, which I found quite useful. Cheers ,Jay
Great post, thanks for the info
thx!
The topic is quite hot in the net right now. What do you pay the most attention to when choosing what to write about?
nice and easy tutorial.
thanks for the great post.
[...] View Tutorial No Comment var addthis_pub=”izwan00″; BOOKMARK This entry was posted on Saturday, June 6th, 2009 at 6:08 am and is filed under Php Tutorials. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site. [...]
As an alternative I use NetBeans to write code and I debug it with Google Chrome. Here is a very short setup:
http://arturito.net/2011/05/21/local-and-remote-php-debuging-in-netbeans-with-xdebug-on-google-chrome-just-like-in-visual-studio/
Nice tutorial, I’ve used firebug for a while now but I never knew about firePHP. Thanks and keep up the great work.
Someone essentially lend a hand to make seriously posts I would state. This is the first time I frequented your web page and so far? I amazed with the research you made to make this actual put up amazing. Fantastic task!