
- Drop me a line! Contact me here.
Debugging PHP Scripts
Wouldn't it be great if code just worked first time? That'd be brilliant. No testing, no fixing, just write the stuff and it does what it's supposed to. Lovely.
Also impossible though. Code never works first time. This is why the debugging process is important. Testing, fixing, testing again, fixing some more, it's a critical aspect of development. It's an aspect that newer developers often fail to spend anywhere near enough time on too. There's a guideline that states software development should follow a 40-20-40 structure - 40% of the project should be planning, 20% should be development (coding) and 40% should be testing and fixing things. Sadly it doesn't work out like that.
Still, there are a few things that can speed up the testing process. Here's my top 5:
5. Write comments.
Comments are the first thing to be left out if you're dashing off a section of code, or you're stressed, or if something doesn't seem important. This will come back to haunt you. What seems incredibly simple and obvious now might seem like alien spaghetti code in 6 months time. What's easy for you to understand might be complicated for the next person to visit your application.
Once you've gotten into the habit of writing comments you need to learn what to comment. Something that just describes what some code is doing isn't very useful. For example:
The comment tells you nothing that isn't obvious from the code. A useful comment should tell anyone reading the code why the code does what it does.
You can also document your code by adding inline document comments. The best known method is using PHPDocumentor. The PHPDocumentor application reads specially formatted comments in your code and turns them into nicely formatted HTML documentation. It's an interesting idea and possibly worthwhile if you use it extensively, but I've found it slows down development time a little too much.
4. Check your PHP configuration.
Occasionally a problem with a script will arise from the configuration of PHP on the server, especially when you're having problems with file uploads. php_info() is a useful function that produces a page of information about the server config. This should be your first port of call for configuration issues.
If the problem is specifically with file uploads and trying to upload a large file then this code will report exactly what the configuration is, even accounting for runtime changes to the settings that have been made with a .htaccess file.
Also impossible though. Code never works first time. This is why the debugging process is important. Testing, fixing, testing again, fixing some more, it's a critical aspect of development. It's an aspect that newer developers often fail to spend anywhere near enough time on too. There's a guideline that states software development should follow a 40-20-40 structure - 40% of the project should be planning, 20% should be development (coding) and 40% should be testing and fixing things. Sadly it doesn't work out like that.
Still, there are a few things that can speed up the testing process. Here's my top 5:
5. Write comments.
Comments are the first thing to be left out if you're dashing off a section of code, or you're stressed, or if something doesn't seem important. This will come back to haunt you. What seems incredibly simple and obvious now might seem like alien spaghetti code in 6 months time. What's easy for you to understand might be complicated for the next person to visit your application.
Once you've gotten into the habit of writing comments you need to learn what to comment. Something that just describes what some code is doing isn't very useful. For example:
<?php
for ($intCounter = 0; $intCounter < 10; $intCounter++) { //Counts up to 9
echo $intCounter;
}
?>
The comment tells you nothing that isn't obvious from the code. A useful comment should tell anyone reading the code why the code does what it does.
You can also document your code by adding inline document comments. The best known method is using PHPDocumentor. The PHPDocumentor application reads specially formatted comments in your code and turns them into nicely formatted HTML documentation. It's an interesting idea and possibly worthwhile if you use it extensively, but I've found it slows down development time a little too much.
4. Check your PHP configuration.
Occasionally a problem with a script will arise from the configuration of PHP on the server, especially when you're having problems with file uploads. php_info() is a useful function that produces a page of information about the server config. This should be your first port of call for configuration issues.
If the problem is specifically with file uploads and trying to upload a large file then this code will report exactly what the configuration is, even accounting for runtime changes to the settings that have been made with a .htaccess file.
<?php
echo 'version: ', phpversion(), "<br />\n";
echo 'sapi: ', php_sapi_name(), "<br />\n";
foreach( array(
'max_execution_time', 'max_input_time',
'memory_limit', 'post_max_size',
'upload_tmp_dir', 'upload_max_filesize')
as $p ) {
echo $p, ': ', get_cfg_var($p), "<br />\n";
}
?>
Comments
Comments are not currently being accepted for this article.
Sidebar
Published:
22/06/2007
Views:
3446
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.0919 seconds
