How to debug the PHP code !

Talk about anything in here.
Post Reply
kpkguru003
Posts: 14
Joined: Fri Nov 11, 2011 12:32 pm
Location: India

How to debug the PHP code !

Post by kpkguru003 »

i feel really tough to debug the PHP code , because i am making many silly mistakes which i cant find quickly !
is there any way to debug the code ?
User avatar
Kamal
Posts: 123
Joined: Fri May 06, 2011 10:45 am
Contact:

Re: How to debug the PHP code !

Post by Kamal »

There is a debugger called xDebug, try it :)
Tino
Posts: 360
Joined: Thu May 05, 2011 8:55 pm
Location: The Netherlands

Re: How to debug the PHP code !

Post by Tino »

Yes, there is a way, make sure error reporting is turned on and that all your errors are being displayed. Don't do the debugger thing. You won't learn a whole lot from that.
error_reporting(E_ALL);
ini_set('display_errors', 1);
Every syntactical (spelling?) error you make should be displayed, and you should be able to debug your problem with the information you get from the error. The same goes for fatal errors like a failing mysql query.

Logical errors are more difficult. Basically you should check what is going wrong, then find the piece of code that is associated to it. If that piece of code looks good, find the code that leads to the first piece of code and check if that's doing what you want/expect it to do, and so on until you find the problem. It can be quite difficult, because some logic mistakes can be very subtle and therefore very difficult to find.

And if you really can't figure it out... post your problem on the forum! ;)
Please check out my CodeCanyon items.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: How to debug the PHP code !

Post by jacek »

Equally hard to spot
if (1 === 7);{
    echo 'hello';
}
The ; after the closing ) makes the code effectively mean this
if (1 === 7){

}

echo 'hello';
which can cause some pretty confusing results.

But for the simpler things like syntax errors the error message tells you everything you need to know, they can be a little hard to understand sometimes but for that you just need to practice. A lot of people post saying that that have completely started again with a tutorial X number of times. That is completely the wrong thing to do, it's the debugging experience that is the most rewarding, and also the most important in terms of getting better and learning. you have to learn how to solve the problems, when you are working on your own project you can't just look at some reference code to see if you typed it right.
Image
Post Reply