By Marcel

2008-08-03 23:18:21 8 Comments

How do you debug PHP scripts?

I am aware of basic debugging such as using the Error Reporting. The breakpoint debugging in PHPEclipse is also quite useful.

What is the best (in terms of fast and easy) way to debug in phpStorm or any other IDE?


@kenorb 2016-03-21 12:34:17


The Interactive Stepthrough PHP Debugger implemented as a SAPI module which can give give you complete control over the environment without impacting the functionality or performance of your code. It aims to be a lightweight, powerful, easy to use debugging platform for PHP 5.4+ and it's shipped out-of-box with PHP 5.6.

Features includes:

  • Stepthrough Debugging
  • Flexible Breakpoints (Class Method, Function, File:Line, Address, Opcode)
  • Easy Access to PHP with built-in eval()
  • Easy Access to Currently Executing Code
  • Userland API
  • SAPI Agnostic - Easily Integrated
  • PHP Configuration File Support
  • JIT Super Globals - Set Your Own!!
  • Optional readline Support - Comfortable Terminal Operation
  • Remote Debugging Support - Bundled Java GUI
  • Easy Operation

See the screenshots:

PHP DBG - Stepthrough Debugging - screenshot

PHP DBG - Stepthrough Debugging - screenshot

Home page:

PHP Error - Better error reporting for PHP

This is very easy to use library (actually a file) to debug your PHP scripts.

The only thing that you need to do is to include one file as below (at the beginning on your code):


Then all errors will give you info such as backtrace, code context, function arguments, server variables, etc. For example:

PHP Error | Improve Error Reporting for PHP - screenshot of backtrace PHP Error | Improve Error Reporting for PHP - screenshot of backtrace PHP Error | Improve Error Reporting for PHP - screenshot of backtrace

Features include:

  • trivial to use, it's just one file
  • errors displayed in the browser for normal and ajaxy requests
  • AJAX requests are paused, allowing you to automatically re-run them
  • makes errors as strict as possible (encourages code quality, and tends to improve performance)
  • code snippets across the whole stack trace
  • provides more information (such as full function signatures)
  • fixes some error messages which are just plain wrong
  • syntax highlighting
  • looks pretty!
  • customization
  • manually turn it on and off
  • run specific sections without error reporting
  • ignore files allowing you to avoid highlighting code in your stack trace
  • application files; these are prioritized when an error strikes!

Home page:


My fork (with extra fixes):


If your system supports DTrace dynamic tracing (installed by default on OS X) and your PHP is compiled with the DTrace probes enabled (--enable-dtrace) which should be by default, this command can help you to debug PHP script with no time:

sudo dtrace -qn 'php*:::function-entry { printf("%Y: PHP function-entry:\t%s%s%s() in %s:%d\n", walltimestamp, copyinstr(arg3), copyinstr(arg4), copyinstr(arg0), basename(copyinstr(arg1)), (int)arg2); }'

So given the following alias has been added into your rc files (e.g. ~/.bashrc, ~/.bash_aliases):

alias trace-php='sudo dtrace -qn "php*:::function-entry { printf(\"%Y: PHP function-entry:\t%s%s%s() in %s:%d\n\", walltimestamp, copyinstr(arg3), copyinstr(arg4), copyinstr(arg0), basename(copyinstr(arg1)), (int)arg2); }"'

you may trace your script with easy to remember alias: trace-php.

Here is more advanced dtrace script, just save it into dtruss-php.d, make it executable (chmod +x dtruss-php.d) and run:

#!/usr/sbin/dtrace -Zs
# See:

#pragma D option quiet

    printf("%Y: PHP compile-file-entry:\t%s (%s)\n", walltimestamp, basename(copyinstr(arg0)), copyinstr(arg1));

    printf("%Y: PHP compile-file-return:\t%s (%s)\n", walltimestamp, basename(copyinstr(arg0)), basename(copyinstr(arg1)));

    printf("%Y: PHP error message:\t%s in %s:%d\n", walltimestamp, copyinstr(arg0), basename(copyinstr(arg1)), (int)arg2);

    printf("%Y: PHP exception-caught:\t%s\n", walltimestamp, copyinstr(arg0));

    printf("%Y: PHP exception-thrown:\t%s\n", walltimestamp, copyinstr(arg0));

    printf("%Y: PHP execute-entry:\t%s:%d\n", walltimestamp, basename(copyinstr(arg0)), (int)arg1);

    printf("%Y: PHP execute-return:\t%s:%d\n", walltimestamp, basename(copyinstr(arg0)), (int)arg1);

    printf("%Y: PHP function-entry:\t%s%s%s() in %s:%d\n", walltimestamp, copyinstr(arg3), copyinstr(arg4), copyinstr(arg0), basename(copyinstr(arg1)), (int)arg2);

    printf("%Y: PHP function-return:\t%s%s%s() in %s:%d\n", walltimestamp, copyinstr(arg3), copyinstr(arg4), copyinstr(arg0), basename(copyinstr(arg1)), (int)arg2);

    printf("%Y: PHP request-shutdown:\t%s at %s via %s\n", walltimestamp, basename(copyinstr(arg0)), copyinstr(arg1), copyinstr(arg2));

    printf("%Y, PHP request-startup:\t%s at %s via %s\n", walltimestamp, basename(copyinstr(arg0)), copyinstr(arg1), copyinstr(arg2));

Home page: dtruss-lamp at GitHub

Here is simple usage:

  1. Run: sudo dtruss-php.d.
  2. On another terminal run: php -r "phpinfo();".

To test that, you can go to any docroot with index.php and run PHP builtin server by:

php -S localhost:8080

After that you can access the site at http://localhost:8080/ (or choose whatever port is convenient for you). From there access some pages to see the trace output.

Note: Dtrace is available on OS X by default, on Linux you probably need dtrace4linux or check for some other alternatives.

See: Using PHP and DTrace at


Alternatively check for SystemTap tracing by installing SystemTap SDT development package (e.g. yum install systemtap-sdt-devel).

Here is example script (all_probes.stp) for tracing all core PHP static probe points throughout the duration of a running PHP script with SystemTap:

probe process("sapi/cli/php").provider("php").mark("compile__file__entry") {
    printf("Probe compile__file__entry\n");
    printf("  compile_file %s\n", user_string($arg1));
    printf("  compile_file_translated %s\n", user_string($arg2));
probe process("sapi/cli/php").provider("php").mark("compile__file__return") {
    printf("Probe compile__file__return\n");
    printf("  compile_file %s\n", user_string($arg1));
    printf("  compile_file_translated %s\n", user_string($arg2));
probe process("sapi/cli/php").provider("php").mark("error") {
    printf("Probe error\n");
    printf("  errormsg %s\n", user_string($arg1));
    printf("  request_file %s\n", user_string($arg2));
    printf("  lineno %d\n", $arg3);
probe process("sapi/cli/php").provider("php").mark("exception__caught") {
    printf("Probe exception__caught\n");
    printf("  classname %s\n", user_string($arg1));
probe process("sapi/cli/php").provider("php").mark("exception__thrown") {
    printf("Probe exception__thrown\n");
    printf("  classname %s\n", user_string($arg1));
probe process("sapi/cli/php").provider("php").mark("execute__entry") {
    printf("Probe execute__entry\n");
    printf("  request_file %s\n", user_string($arg1));
    printf("  lineno %d\n", $arg2);
probe process("sapi/cli/php").provider("php").mark("execute__return") {
    printf("Probe execute__return\n");
    printf("  request_file %s\n", user_string($arg1));
    printf("  lineno %d\n", $arg2);
probe process("sapi/cli/php").provider("php").mark("function__entry") {
    printf("Probe function__entry\n");
    printf("  function_name %s\n", user_string($arg1));
    printf("  request_file %s\n", user_string($arg2));
    printf("  lineno %d\n", $arg3);
    printf("  classname %s\n", user_string($arg4));
    printf("  scope %s\n", user_string($arg5));
probe process("sapi/cli/php").provider("php").mark("function__return") {
    printf("Probe function__return: %s\n", user_string($arg1));
    printf(" function_name %s\n", user_string($arg1));
    printf("  request_file %s\n", user_string($arg2));
    printf("  lineno %d\n", $arg3);
    printf("  classname %s\n", user_string($arg4));
    printf("  scope %s\n", user_string($arg5));
probe process("sapi/cli/php").provider("php").mark("request__shutdown") {
    printf("Probe request__shutdown\n");
    printf("  file %s\n", user_string($arg1));
    printf("  request_uri %s\n", user_string($arg2));
    printf("  request_method %s\n", user_string($arg3));
probe process("sapi/cli/php").provider("php").mark("request__startup") {
    printf("Probe request__startup\n");
    printf("  file %s\n", user_string($arg1));
    printf("  request_uri %s\n", user_string($arg2));
    printf("  request_method %s\n", user_string($arg3));


stap -c 'sapi/cli/php test.php' all_probes.stp

See: Using SystemTap with PHP DTrace Static Probes at

@John Downey 2008-08-03 23:28:39

Try Eclipse PDT to setup an Eclipse environment that has debugging features like you mentioned. The ability to step into the code is a much better way to debug then the old method of var_dump and print at various points to see where your flow goes wrong. When all else fails though and all I have is SSH and vim I still var_dump()/die() to find where the code goes south.

@AlexMorley-Finch 2012-04-05 00:05:10

You should use this function: kill( $data ) { die( var_dump ( $data ) ); } It saves typing 10 characters, best function i have ever written tbh:)

@Tomáš Fejfar 2014-02-17 10:09:46

@RPDeshaies 2014-03-12 20:36:18

Is there a way to beautify the "var_dump" ?

@Dan 2014-04-24 21:42:50

@Tareck117 Yes. You can install xdebug

@jason 2014-09-29 16:16:29

@Tareck an alternative to prettify debug output : $v2 = array ("h"=>5, "o"=>10, "p"=>11); $str = serialize($v2); echo "<pre>" .print_r(unserialize($str), true) . "</pre>";

@Konstantin 2014-11-29 08:57:57

@Tareck117 Yes, use var_export instead

@Francisco Presencia 2015-01-04 19:29:24

@AlexMorley-Finch I raise you to kill($data) { echo "<pre>"; var_dump($data); echo "</pre>"; exit; }

@Gruber 2016-02-01 21:21:13

Link is "recoverable" via the incredible Web Archive, last check as of 7 may '15.

@Todd 2016-03-02 05:40:03

Don't bother with the Web Archive, since this answer was created Eclipse have created a proper PHP Development Environment - I have updated the answer to suit.

@Pat 2008-08-05 17:22:09

You can use Firephp an add-on to firebug to debug php in the same environment as javascript.

I also use Xdebug mentioned earlier for profiling php.

@Mihaela 2014-10-17 10:29:54

And here's a quick guide to using FirePHP:

@shekh danishuesn 2015-10-01 11:16:08

There are many PHP debugging techniques that can save you countless hours when coding. An effective but basic debugging technique is to simply turn on error reporting. Another slightly more advanced technique involves using print statements, which can help pinpoint more elusive bugs by displaying what is actually going onto the screen. PHPeclipse is an Eclipse plug-in that can highlight common syntax errors and can be used in conjunction with a debugger to set breakpoints.

display_errors = Off
error_reporting = E_ALL 
display_errors = On

and also used


@Julio César 2008-08-22 15:43:34

XDebug is essential for development. I install it before any other extension. It gives you stack traces on any error and you can enable profiling easily.

For a quick look at a data structure use var_dump(). Don't use print_r() because you'll have to surround it with <pre> and it only prints one var at a time.

<?php var_dump(__FILE__, __LINE__, $_REQUEST); ?>

For a real debugging environment the best I've found is Komodo IDE but it costs $$.

@eisberg 2009-06-29 13:40:30

This is my little debug environment:

assert_options(ASSERT_ACTIVE, 1);
assert_options(ASSERT_WARNING, 0);
assert_options(ASSERT_BAIL, 0);
assert_options(ASSERT_QUIET_EVAL, 0);
assert_options(ASSERT_CALLBACK, 'assert_callcack');

function assert_callcack($file, $line, $message) {
    throw new Customizable_Exception($message, null, $file, $line);

function error_handler($errno, $error, $file, $line, $vars) {
    if ($errno === 0 || ($errno & error_reporting()) === 0) {

    throw new Customizable_Exception($error, $errno, $file, $line);

function exception_handler(Exception $e) {
    // Do what ever!
    echo '<pre>', print_r($e, true), '</pre>';

function shutdown_handler() {
    try {
        if (null !== $error = error_get_last()) {
            throw new Customizable_Exception($error['message'], $error['type'], $error['file'], $error['line']);
    } catch (Exception $e) {

class Customizable_Exception extends Exception {
    public function __construct($message = null, $code = null, $file = null, $line = null) {
        if ($code === null) {
        } else {
            parent::__construct($message, $code);
        if ($file !== null) {
            $this->file = $file;
        if ($line !== null) {
            $this->line = $line;

@Sec 2013-06-05 15:24:19

Thank you. That saved my day. (I just had to remove that E_STRICT)

@Madbreaks 2015-12-28 22:48:27

assert_callcack heh

@Mufaddal 2012-05-29 12:43:39

Nusphere is also a good debugger for php nusphere

@Jannie Theunissen 2010-07-09 03:14:15

I use Netbeans with XDebug and the Easy XDebug FireFox Add-on

The add-on is essential when you debug MVC projects, because the normal way XDebug runs in Netbeans is to register the dbug session via the url. With the add-on installed in FireFox, you would set your Netbeans project properties -> Run Configuratuion -> Advanced and select "Do Not Open Web Browser" You can now set your break points and start the debugging session with Ctrl-F5 as usual. Open FireFox and right-click the Add-on icon in the right bottom corner to start monitoring for breakpoints. When the code reaches the breakpoint it will stop and you can inspect your variable states and call-stack.

@MintDeparture 2010-05-10 09:29:24

I often use CakePHP when Rails isn't possible. To debug errors I usually find the error.log in the tmp folder and tail it in the terminal with the command...

tail -f app/tmp/logs/error.log

It give's you running dialog from cake of what is going on, which is pretty handy, if you want to output something to it mid code you can use.


This can usually give you a good idea of what is going on/wrong.

@Petr Peller 2010-05-10 09:18:59

The most of bugs can be found easily by simply var_dumping some of key variables, but it obviously depends on what kind of application you develop.

For a more complex algorithms the step/breakpoint/watch functions are very helpful (if not necessary)

@jlleblanc 2008-08-07 00:25:13

1) I use print_r(). In TextMate, I have a snippet for 'pre' which expands to this:

echo "<pre>";
echo "</pre>";

2) I use Xdebug, but haven't been able to get the GUI to work right on my Mac. It at least prints out a readable version of the stack trace.

@altermativ 2009-07-29 13:18:37

I'm sure you mean echo "</pre>"; at the end though.

@jlleblanc 2009-07-29 22:17:01

Yes, I did. Thanks for catching it! Now corrected.

@DisgruntledGoat 2010-07-21 11:24:07

You can also pass 'true' into the function so it returns the string. It means you can do this: echo '<pre>', print_r($var, true), '</pre>';

@monk.e.boy 2009-02-05 09:16:41

PhpEd is really good. You can step into/over/out of functions. You can run ad-hoc code, inspect variables, change variables. It is amazing.

@siliconrockstar 2013-12-13 16:42:28

I have used PhpEd and I have no kind words for it when compared to a real IDE like NetBeans or Eclipse, nor does this comment add anything useful to the question. -1

@lm713 2015-06-24 11:57:44

I tried most of the IDEs (including Zend, NetBeans and Eclipse) before buying PhpED Professional because it was the best by a mile and a half. This was a few years ago, so the others may have since improved, but at the time most of them were painfully slow because they were written in Java. I don't understand how someone can have "no kind words for it" when (for me) it was so clearly the best, the decision was a no-brainer.

@Ken 2008-10-22 09:16:27

Output buffering is very useful if you don't want to mess up your output. I do this in a one-liner which I can comment/uncomment at will

 ob_start();var_dump(); user_error(ob_get_contents()); ob_get_clean();

@Arvind K. 2013-10-15 07:08:35

@Joshi Spawnbrood 2008-10-22 08:46:43

Usually I find create a custom log function able to save on file, store debug info, and eventually re-print on a common footer.

You can also override common Exception class, so that this type of debugging is semi-automated.

@Nathan Strong 2008-09-18 03:17:36

Well, to some degree it depends on where things are going south. That's the first thing I try to isolate, and then I'll use echo/print_r() as necessary.

NB: You guys know that you can pass true as a second argument to print_r() and it'll return the output instead of printing it? E.g.:

echo "<pre>".print_r($var, true)."</pre>";

@jdelator 2008-10-30 22:03:58

I just wrap that in a function called debug. So then I can do debug($var);

@Toby Allen 2008-09-17 10:14:47

PhpEdit has a built in debugger, but I usually end up using echo(); and print_r(); the old fashioned way!!

@djn 2008-09-15 20:23:29

Xdebug and the DBGp plugin for Notepad++ for heavy duty bug hunting, FirePHP for lightweight stuff. Quick and dirty? Nothing beats dBug.

@Joe 2013-08-12 12:46:08

The DBGp plugin doesn't work with the current version of notepad++/xdebug and there are no plans to fix it. You can see my discussion with the creator linked here

@Nacho 2008-08-26 15:04:57

I use Netbeans with XDebug. Check it out at its website for docs on how to configure it.

@Michael Luton 2008-08-22 20:10:54

The integrated debuggers where you can watch the values of variable change as you step through code are really cool. They do, however, require software setup on the server and a certain amount of configuration on the client. Both of which require periodic maintenance to keep in good working order.

A print_r is easy to write and is guaranteed to work in any setup.

@amit 2008-08-22 15:44:33

Komodo IDE works well with xdebug, even for the remore debugging. It needs minimum amount of configuration. All you need is a version of php that Komodo can use locally to step through the code on a breakpoint. If you have the script imported into komodo project, then you can set breakpoints with a mouse-click just how you would set it inside eclipse for debugging a java program. Remote debugging is obviously more tricky to get it to work correctly ( you might have to map the remote url with a php script in your workspace ) than a local debugging setup which is pretty easy to configure if you are on a MAC or a linux desktop.

@Ross 2008-08-22 15:36:59

Manual debugging is generally quicker for me - var_dump() and debug_print_backtrace() are all the tools you need to arm your logic with.

@Karim 2008-08-22 15:33:44

For the really gritty problems that would be too time consuming to use print_r/echo to figure out I use my IDE's (PhpEd) debugging feature. Unlike other IDEs I've used, PhpEd requires pretty much no setup. the only reason I don't use it for any problems I encounter is that it's painfully slow. I'm not sure that slowness is specific to PhpEd or any php debugger. PhpEd is not free but I believe it uses one of the open-source debuggers (like XDebug previously mentioned) anyway. The benefit with PhpEd, again, is that it requires no setup which I have found really pretty tedious in the past.

@Artem Russakovskii 2010-01-27 02:30:38

The PHPEd debugger is actually written by the same guy who wrote PHPEd and I'm pretty sure it's not open source. At least PHPEd doesn't ship with the source but instead compiled .so's and .dll's.

@Brendon-Van-Heyzen 2008-08-17 18:38:01

i use zend studio for eclipse with the built in debugger. Its still slow compared to debugging with eclipse pdt with xdebug. Hopefully they will fix those issues, the speed has improved over the recent releases but still stepping over things takes 2-3 seconds. The zend firefox toolbar really makes things easy (debug next page, current page, etc). Also it provides a profiler that will benchmark your code and provide pie-charts, execution time, etc.

@Dan Grover 2008-08-15 04:23:02

In a production environment, I log relevant data to the server's error log with error_log().

@markus_p 2012-08-16 22:28:44

and than tail -f ... works great

@Eric Lamb 2008-08-06 15:46:25

Depending on the issue I like a combination of error_reporting(E_ALL) mixed with echo tests (to find the offending line/file the error happened in initally; you KNOW it's not always the line/file php tells you right?), IDE brace matching (to resolve "Parse error: syntax error, unexpected $end" issues), and print_r(); exit; dumps (real programmers view the source ;p).

You also can't beat phpdebug (check sourceforge) with "memory_get_usage();" and "memory_get_peak_usage();" to find the problem areas.

@Polsonby 2008-08-05 00:49:42

+1 for print_r(). Use it to dump out the contents of an object or variable. To make it more readable, do it with a pre tag so you don't need to view source.

echo '<pre>';

Also var_dump($thing) - this is very useful to see the type of subthings

@Arvind K. 2013-10-15 07:07:15

@Arvind K. 2013-10-15 07:08:11

@Teifion 2008-08-04 21:28:44

In all honesty, a combination of print and print_r() to print out the variables. I know that many prefer to use other more advanced methods but I find this the easiest to use.

I will say that I didn't fully appreciate this until I did some Microprocessor programming at Uni and was not able to use even this.

@Brad 2008-10-22 12:14:01

I am glad you mentioned print as well as print_r, I use a basic print to see if the code executed to a certain point, which helps isolating the problem.

@Joshua K 2009-09-09 15:38:28

I use both print and var_dump(). I use print to display debug messages and information and var_dump to indicate the state of variables as things progress.

@Christian Lescuyer 2008-08-04 21:07:09

Xdebug, by Derick Rethans, is very good. I used it some time ago and found it was not so easy to install. Once you're done, you won't understand how you managed without it :-)

There is a good article on Zend Developer Zone (installing on Linux doesn't seem any easier) and even a Firefox plugin, which I never used.

@Jahangir 2008-09-22 14:34:53

Its not just installing that's frustrating. Configuring Xdebug to work with Eclipse can be a nightmare. I was able to get Xdebug installed on CentOS 5 but EclipsePDT+Xdebug dont want to co-operate :(

@Orion Edwards 2008-08-03 23:32:13

print_r( debug_backtrace() );

or something like that :-)

@Arvind K. 2013-10-15 07:07:45

@Michael Stum 2008-08-03 23:20:48

I've used the Zend Studio (5.5), together with Zend Platform. That gives proper debugging, breakpoints/stepping over the code etc., although at a price.

Related Questions

Sponsored Content

29 Answered Questions

[SOLVED] How Do You Parse and Process HTML/XML in PHP?

28 Answered Questions

[SOLVED] How can I prevent SQL injection in PHP?

7 Answered Questions

[SOLVED] How does PHP 'foreach' actually work?

35 Answered Questions

[SOLVED] How do I debug Node.js applications?

32 Answered Questions

[SOLVED] Reference - What does this error mean in PHP?

18 Answered Questions

[SOLVED] Reference — What does this symbol mean in PHP?

30 Answered Questions

[SOLVED] Convert HTML + CSS to PDF with PHP?

29 Answered Questions

[SOLVED] How to get useful error messages in PHP?

9 Answered Questions

14 Answered Questions

[SOLVED] Secure hash and salt for PHP passwords

Sponsored Content