Articles in Basics Category

Basic PHP Debugging

Debugging means tracing down errors in your programs and correcting them. PHP doesn’t come with a built-in debugger. PHP manual recommends some third party tools for debugging.

However for basic debugging, we can use few functions available in PHP. These can be used to troubleshoot most of the errors you get in PHP.

Setting Break Points

One challenge in debugging is to find out at which point the error occurred. Since the execution of a PHP script can involve function calls, loops, control structures and including other files, it would be difficult to figure out the point by just looking at the code.

die() lets you stop the execution of a PHP script at a certain point since PHP interpreter stops after seeing a die() statement. This lets you see whether a certain part of your program is really being executed.

For an example think that you expect a certain If condition to be executed but not sure whether it happens. Then you can place a die() statement as below and clear the doubt.

if (!empty($days)) {

	die('Point 1: $days is not empty');

	// More code may go here


After running the file that contains above code segment, if you don’t see Point 1: $days is not empty then you can think that execution doesn’t come inside the control structure probably because $days is empty.

If you see the message then it’s probably because of the code inside the control structure that you are not getting the expected result. So, you can remove the current die() statement and place it near another code segment that you suspect.

Debugging Functions

var_dump() and print_r() are two functions that you can use to check values. var_dump() shows the value and its type while print_r() only shows the value.


$day = 'Monday';

echo '<br /><br />';


After running above code, it will output following lines in your browser. You can see that var_dump() showed more information while print_r() showed only the value.

string 'Monday' (length=6)


These two come handy when you need to check arrays. In following example, <pre> tags have been used for better formatting of the output. Otherwise the output will be printed in lines which may not be readable. If you have installed Xdebug extension then formatting will automatically be applied to var_dump().


$days = array('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday');

echo '<pre>';
echo '<br /><br />';
echo '</pre>';


Output of above code will be like below.

array(7) {
  string(6) "Monday"
  string(7) "Tuesday"
  string(9) "Wednesday"
  string(8) "Thursday"
  string(6) "Friday"
  string(8) "Saturday"
  string(6) "Sunday"

    [0] => Monday
    [1] => Tuesday
    [2] => Wednesday
    [3] => Thursday
    [4] => Friday
    [5] => Saturday
    [6] => Sunday

You may think what’s the use of print_r() over var_dump() since var_dump() shows more information along with the values. However there may be cases where you want to check values of complex multidimensional arrays and that you find more information var_dump() provides is cluttering.

There is also another function called var_export() which is not commonly used. It’s similar to print_r() but its output is valid PHP code.

echo '<pre>';
echo '</pre>';

Above code will output the array definition of $days as below.

array (
  0 => 'Monday',
  1 => 'Tuesday',
  2 => 'Wednesday',
  3 => 'Thursday',
  4 => 'Friday',
  5 => 'Saturday',
  6 => 'Sunday',

Remember to Clear Debugging Code

Forgetting to remove debugging code can lead to show unexpected output to the users. Therefore after troubleshooting, always remember to clear code used for debugging.

Where to Head from Here...
Share with Your Peers...

We Value Your Feedback...

We love to hear what you think about this article. Please provide your opinion, suggestions and improvements using following form. Note that submitted feedback is not displayed but we will get back to you if it needs a reply.