Find Errors Quickly: Simple PHP Code Debugging

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!

php_value display_errors 1
php_value error_reporting E_ALL

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:

$conn = mysqli_connect("localhost", "my_user", "my_password", "my_database");

if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
} else {
	if ($result = mysqli_query($conn, "SELECT * FROM table1 WHERE id = 34")) {
		// do somtehing with the result...
    } else {
		 printf("Error: %s\n", mysqli_error($conn));
	}
}

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:

if (mail('you@mail.com', 'some subject', 'some message')) {
	echo 'mail send';
} else {
	echo 'mail error';
}

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:

$arr = @getimagesize($image);

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!

Published in: PHP Scripts