Page 1 of 1

How to debug the PHP code !

Posted: Tue Dec 20, 2011 7:58 am
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 ?

Re: How to debug the PHP code !

Posted: Tue Dec 20, 2011 10:56 am
by Kamal
There is a debugger called xDebug, try it :)

Re: How to debug the PHP code !

Posted: Tue Dec 20, 2011 4:11 pm
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.

[syntax=php]error_reporting(E_ALL);
ini_set('display_errors', 1);[/syntax]

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! ;)

Re: How to debug the PHP code !

Posted: Wed Dec 21, 2011 12:54 am
by jacek
Equally hard to spot

[syntax=php]if (1 === 7);{
echo 'hello';
}[/syntax]
The ; after the closing ) makes the code effectively mean this

[syntax=php]if (1 === 7){

}

echo 'hello';[/syntax]
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.