Simple PHP Code Debugging

Share with your friends








Submit

Each beginning (and more advanced) PHP coder will fail because of errors while writing PHP code. This post will teach you some simple things about how you can find those errors in your PHP script. By default some errors will not show up on your web server!

PHP code debugging

Try the examples from the PHP manual

The PHP manual is one of the best manuals I’ve ever read, each function is indexed and contains comments and examples from users. Often you will find exact the code snippet you need. Tip! Just add the function name behind the domain for a quick access like: http://www.php.net/foreach In most cases the PHP configuration does not allow to to show the errors in the production environment. Enable (temporary) a full error report with this two directives inside your .htaccess file. Don’t do this on a busy website, test your code on a test location first!

If you test your database powered website there are often problems with dynamic variables within the SQL statement. Test ALWAYS your statement in phpMyAdmin or use at least some error reporting like:

Your PHP code has missing (curly) brackets

If your PHP script becomes bigger and bigger it’s almost normal that you will forget to close some brackets. Follow this two rules to make your live easier:

  • Indent your code, each “tab” presents a bracket you have used before
  • Comment your closed brackets like \\ end if result is not empty

There are a lot of PHP code editors offering a “close bracket” feature/mark-up. Test your code with “IF” statements, most PHP functions return a “false” if the function has failed (check the manual for the valid return value). For example:

You can do this check on all positions were something might be wrong. Another way to check what your PHP code is doing, is to show the output of some variable, array or object. Use function like “print_r()” or “var_dump()” to show the values and keys from arrays and objects.

The white screen of death!

A blank screen is really bad and will eat most of your time! Test your code with “IF” statements and show some message with echo ‘ok’;  to find out where the error is located. If you have access to the error_log from your web hosting account or server. Another problem while finding find errors is that some developer adds the “@” error control operator before some functions. If this happens, a possible error generated by this function is not reported, for example:

Don’t use the “@” error control operator if you test your script!

If you have some error message you can’t fix, try a search on Google for a part of this error message or ask other PHP programmers on a PHP forum. There is always someone who knows this error message, because the error message you get is programmed by some other developer!

Comment Rules

Don’t post your code here, post your code block or snippet to pastebin and include the pastebin URL in your comment.

I delete all comments with non related links inside the comment text. Don't use keywords for the field of your real name (most people like to use your name for their answer). Keep your comment related to the topic, if your question is off-topic, please use the contact form instead.

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*
Website