|
||||||
| PHP Tutorials PHP Tutorials |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||||
|
Everyone makes errors and if you use PHP you will create bugs. The purpose of this tutorial is to give an introduction to debugging using PHP. You will learn basic ways to determine where problems occur as well as functions like var_dump().
First things First When an error occurs you want the the error displayed right on the browser. While your production script may have errors turned off, for debugging purposes you should have this enabled. Follow these steps: 1) Edit your php.ini file. Set the directive display_errors to On. Restart your webserver. Note: If you are on a shared host where you cannot edit your php.ini, and your display_errors directive is set to Off, to achieve the same effect you can use the ini_set() function to enable the directive at runtime. PHP Code:
PHP Code:
Value - Constant 1 -E_ERROR 2 -E_WARNING 4 -E_PARSE 8 -E_NOTICE 16 -E_CORE_ERROR 32 -E_CORE_WARNING 64 -E_COMPILE_ERROR 128 -E_COMPILE_WARNING 256 -E_USER_ERROR 512 -E_USER_WARNING 1024 -E_USER_NOTICE 6143 -E_ALL 2048 -E_STRICT 4096 -E_RECOVERABLE_ERROR You can read more about these at PHP: error_reporting - Manual Note: Turning All Errors Off Even though it isn't mentioned in the PHP: error_reporting manual you can disable error reporting using the value 0. PHP Code:
Parse errors can be one of the most irritating bugs for new PHP programmers to solve. A missing quote ("), bracket ({,}), and a multitude of other errors can be thrown by the interpreter which may leave you asking "What is wrong?". Consider this code: PHP Code:
Code:
Parse error: syntax error, unexpected $end in C:\wamp\www\broken.php on line 5 Line 5: PHP Code:
When an error such as this is cast take a look at the actual line first, in this case ?>. If no immediate errors appear start working your way backwards line-by-line. Do a quick scan of each line making sure you have terminated them correct (;). Make sure matching pairs have an ending pair (in this case the quote). Examples of matching pairs: [ ] { } " " ' ' Now consider the following code: PHP Code:
Code:
Parse error: syntax error, unexpected T_VARIABLE, expecting '(' in C:\wamp\www\broken.php on line 4
Data Errors Data errors are not displayed on your browser with a corresponding line number and there is no way see them. They can be some of the worst bugs that you deal with and may not be seen until months after your program release. They are also the hardest to debug. Start Simple An easy method of debugging is to print out your data. PHP Code:
Code:
http://www.yourdomain.com/myscript.php?function=doevent Code:
Do Event Code:
http://www.yourdomain.com/myscript.php?function=doevnet 1) Print your Variable - In the same code above add print "Function - $function"; just under the $function assignment. This will allow you to see what is actually being passed through the GET value. In the error example above you would of seen an incorrect typing of "doevent" and been able to solve the problem. 2) Dump your variables - A more useful function is var_dump(). Adding var_dump($function); below the $function assignment will produce these results: Code:
string(7) "doevnet" PHP Code:
Code:
array(3) {
[0]=>
int(1)
[1]=>
int(2)
[2]=>
array(3) {
[0]=>
string(1) "a"
[1]=>
string(1) "b"
[2]=>
string(1) "c"
}
}
Use Checks If the user enters nothing for the function the code displays nothing. This isn't very professional. To avoid this and find potential errors always use checks. A check makes sure that a value is filled correctly before executing statements. If function is a required field but the user left it blank an error message needs to be displayed. For this there are several methods: PHP Code:
PHP Code:
Code:
Function Missing. PHP Code:
http://www.yourdomain.com/myscript.php?function=doevnet An error will be seen: PHP Code:
Tips and Tricks 1) Errors can be suppressed by using the @ operator. These are most often found on MySQL functions to prevent login/server information being displayed as a warning. Code:
@mysql_connect(); 3) You can trigger your own errors with the trigger_error() function. Taken from php.net PHP Code:
Example: centricle : Tips for debugging PHP 5) Other userful functions
Conclusion There are many more debugging methods and software that will even assist you in debugging. The methods mentioned here are basic concepts to help you solve bugs aimed at beginners. I hope that this tutorial has helped you and if you have any questions or tips of your own post them here! If nothing else let us know that the tutorial was helpful. Last edited by Jordan; 01-30-2008 at 10:48 AM. |
| Sponsored Links |
|
|
|
|||||
|
Very informative - Great work!
![]()
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum | My Blog Chat with other CodeCall members on IRC; connect to irc.codecall.net and join #codecall |
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| PHP 4 end of life announcement | Jordan | Programming News | 4 | 08-30-2007 09:55 AM |
| PHP Introduction | clookid | PHP Tutorials | 10 | 01-16-2007 07:17 AM |
Goal: 100,000 Posts
Complete: 67%