Simple PHP debugging tips

Whether you are just getting started in PHP development or have been at it for a while, here are some tips that will help you troubleshoot when your code isn't working quite the way it should.

Post a comment if you find these tips useful or would like to share your own. I'd love to hear from you.

Identifying the PHP function being called

The first tip is especially useful if you are working on a larger PHP project where there might be multiple definitions of functions or class methods. You may be surprised to discover that it isn't the function you thought it was calling.

Just insert this code into your PHP file just before you use the function, replacing function_name with the name of the function you want to check. Run your code and it will tell you the exact filename and even the line number for the function that will be called.

For functions

$reflInfo = new ReflectionFunction('function_name');
echo 'Function: '.$reflInfo->getName().'()<br>';
echo 'Lines: '.$reflInfo->getStartLine().' to '.$reflInfo->getEndLine().'<br>';
echo 'File: '. $reflInfo->getFileName();
die();

For class methods

For methods, you will also need to change $this to the class object.

$reflInfo = new ReflectionMethod($this, 'function_name');
echo 'Method: '.$reflInfo->getName().'()<br>';
echo 'Lines: '.$reflInfo->getStartLine().' to '.$reflInfo->getEndLine().'<br>';
echo 'File: '. $reflInfo->getFileName();
die();

More PHP debugging tips coming in the future.


Add a comment:

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