
- Drop me a line! Contact me here.
Debugging PHP Scripts
3. Don't stress over it.
Another key to successful debugging is a clear head. Staring at a problem for hours is unlikely to help. If you can take a break from PHP completely then do it. Alternatively, carry on with a different part of the application you're working on. Trying to solve a problem when you're tired, aggravated or stressed decreases your chances of sorting it out, so don't bother trying if you're annoyed with the script.
Also, while you have to listen to other people's assessment of the urgency of a problem, don't be tempted to hack in a nasty solution to stem the flow complaints, especially if a better one can be written with only a little more time. Quick fixes and hacks ruin code in the long term and will only serve to make things take longer further down the line.
2. Use a second set of eyes.
There's such a thing as 'code blindness'. You can peer at your code for hours on end and not spot a simple problem. The solution to this problem is to share the code with your colleagues, or a mailing list, or an internet forum. Someone else may see what's breaking within seconds despite your best efforts. It's always better to ask a question than spend hours puzzling away on your own.
I recommend PHP Developer's Network. I'm a moderator on there though so I'm rather biased :).
One thing to note - even if your code has no problems it's still worth getting another person to look through it. Something can work and yet have unforeseen problems. The code might have a security issue, or it may not be very scalable. Plus you and whomever else sees the code can learn from what you've written. Doing this in a formal sense is called a "peer review".
1. Don't let the bugs happen in the first place.
It's obvious, and a little facetious to suggest it, but truth be told you can't write perfect code first time as I said at the beginning. However, you can write code that tests whether your code is right or not. This technique is called Unit Testing. In essence a Unit Test is a small application that runs your code and examines the output. If it matches what you've said it ought to match the the test passes and the next test runs. The trick however is to write all the tests before you write the actual application code. That way you can run your tests occasionally to see if everything works as expected. This takes the pain out of running 50 different tests over and over again during development, so you end up testing much more often, and that in itself is bound to improve your code.
There are several Unit Test applications available for PHP, including SimpleTest and PHPUnit. My preference is for SimpleTest. It's a well written and effective piece of kit that should be in every developer's library. Unit Testing is just one part of a methodology called "Test Driven Development" or TDD.
One last note about debugging
Debugging is more the process of fixing bugs. You should actively seek them out through testing. The more you can test something the better it will be. There's a few common things that you really, really must test for in every application:
1. Switch off Javascript. Does the application work without it? Do forms submit? Are inputs validated on the server?
2. Switch off cookies. Does the application work without them? Without sessions? What happens without them?
3. Enter a range of characters in all your form inputs. Do apostrophes break it? Do foreign character sets?
4. Try to upload files that are the wrong type. Do big files work? Do files work even without a file extension?
5. What happens when the site has been running for a while and there's lots of database data? Write a script to generate 500,000 random entries. Is the site still fast enough? Do lists of things try to return every item?
Testing isn't much fun, but it's vital. Don't ignore it.
Another key to successful debugging is a clear head. Staring at a problem for hours is unlikely to help. If you can take a break from PHP completely then do it. Alternatively, carry on with a different part of the application you're working on. Trying to solve a problem when you're tired, aggravated or stressed decreases your chances of sorting it out, so don't bother trying if you're annoyed with the script.
Also, while you have to listen to other people's assessment of the urgency of a problem, don't be tempted to hack in a nasty solution to stem the flow complaints, especially if a better one can be written with only a little more time. Quick fixes and hacks ruin code in the long term and will only serve to make things take longer further down the line.
2. Use a second set of eyes.
There's such a thing as 'code blindness'. You can peer at your code for hours on end and not spot a simple problem. The solution to this problem is to share the code with your colleagues, or a mailing list, or an internet forum. Someone else may see what's breaking within seconds despite your best efforts. It's always better to ask a question than spend hours puzzling away on your own.
I recommend PHP Developer's Network. I'm a moderator on there though so I'm rather biased :).
One thing to note - even if your code has no problems it's still worth getting another person to look through it. Something can work and yet have unforeseen problems. The code might have a security issue, or it may not be very scalable. Plus you and whomever else sees the code can learn from what you've written. Doing this in a formal sense is called a "peer review".
1. Don't let the bugs happen in the first place.
It's obvious, and a little facetious to suggest it, but truth be told you can't write perfect code first time as I said at the beginning. However, you can write code that tests whether your code is right or not. This technique is called Unit Testing. In essence a Unit Test is a small application that runs your code and examines the output. If it matches what you've said it ought to match the the test passes and the next test runs. The trick however is to write all the tests before you write the actual application code. That way you can run your tests occasionally to see if everything works as expected. This takes the pain out of running 50 different tests over and over again during development, so you end up testing much more often, and that in itself is bound to improve your code.
There are several Unit Test applications available for PHP, including SimpleTest and PHPUnit. My preference is for SimpleTest. It's a well written and effective piece of kit that should be in every developer's library. Unit Testing is just one part of a methodology called "Test Driven Development" or TDD.
One last note about debugging
Debugging is more the process of fixing bugs. You should actively seek them out through testing. The more you can test something the better it will be. There's a few common things that you really, really must test for in every application:
1. Switch off Javascript. Does the application work without it? Do forms submit? Are inputs validated on the server?
2. Switch off cookies. Does the application work without them? Without sessions? What happens without them?
3. Enter a range of characters in all your form inputs. Do apostrophes break it? Do foreign character sets?
4. Try to upload files that are the wrong type. Do big files work? Do files work even without a file extension?
5. What happens when the site has been running for a while and there's lots of database data? Write a script to generate 500,000 random entries. Is the site still fast enough? Do lists of things try to return every item?
Testing isn't much fun, but it's vital. Don't ignore it.
Comments
Comments are not currently being accepted for this article.
Sidebar
Published:
22/06/2007
Views:
3443
Author:
Chris Neale
Labels:
Print:
Sidebar:
- © Ooer.com Chris Neale 2007
- PHPGD.com
- Powered by PHP
- Database by MySQL
- DB Queries: 9
- DB Time: 0.0043 seconds
