Debugging methods for PHP

More often than not while writing in PHP, you will need to debug the code you have written and/or find out relevant information about classes. This tip outlines different debugging methods such as print_r, var_dump and error logging as well as useful class functions.

print_r();

My most commonly used function in PHP for debugging. It’s great for detailing the contents of arrays and objects. I usually use this function between <pre> tags for formatted output, though not needed if outputting to the CLI.

1
echo '<pre>'.print_r($array, 1).'</pre>';

Note: The second parameter “return” tells PHP to return the output rather than output it itself. If you are going to be doing something with the returned data, for example, echo between <pre> tags, the return parameter is required. On the command line this parameter is not required.

var_dump();

While I tend to use print_r() more than var_dump() it does have its advantage over print_r() Firstly, for variables containing large arrays or objects var_dump() tends to output a great deal faster. Also, var_dump() informs you of the data type of each value in an array and/or object. Fantastic!

1
var_dump($var);

There is no need to use <pre> tags as var_dump() will do the formatting for you.

get_class_methods();

When dealing with objects the get_class_methods() function is really useful. While it wont provide the parameters the objects method accepts it will give a full list of the available, non-magic methods. This combined with print_r(); is really useful:

1
echo '<pre>'.print_r(get_class_methods($obj), 1).'</pre>';

__FILE__ and __LINE__

When you have multiple required files and you’re trying to debug where exactly your script has stopped working __LINE__ and __FILE__ can come in handy.

__FILE__ outputs the current file being parsed by PHP

1
echo 'File: '.__FILE__;

__LINE__ will output the current line of code that is being parsed.

1
echo 'Line: '.__LINE__;

Together these can help narrow down the problematic section of your script.

error_log();

An often useful function to use when debugging, especially when on a live environment is writing to an error log. error_log() accepts four parameters: The message to log, the message type (more on this in a moment), the destination and extra headers.

A basic use of error_log() would be to send the message to PHP’s system logger which is set as an error_log directive in the apache config.

1
error_log('Error message');

If you wish to write the error to a specific file, you can do so by setting the message type to 3 and giving the path to the file. The file will need to be writable.

1
error_log('Error message', 3, '/var/log/site-errors.log');

You can also specify to send the error via email by setting the message type as 1 and the destination as the email address you wish to send the error to.

1
error_log('Error message', 1, 'errors@domain.tld');

When sending an email you can specify extra headers for the email as the fourth parameter.

Keep in mind that error_log() does not append a new line to the error message.

Suggestions:

So there you have it, some useful PHP functions worth considering when debugging. If you have any functions or methods you tend to use day in, day out and feel they should be on this list then feel free to comment.

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>