2

I have a script with alot of nested includes and functions calling each other from lots of if conditions. Basically, its a coding nightmare.

Is there any way i can "PRINT" the PHP code executed ? I mean, print the actual flow of the code and the path taken by the script from start to end ?

hakre
  • 193,403
  • 52
  • 435
  • 836
YD8877
  • 10,401
  • 20
  • 64
  • 92

3 Answers3

2

You can try debug_backtrace() or debug_print_backtrace().

Additionally, I recommend using Xdebug. It prints a very useful stack trace on exceptions (you can configure it to print out every method parameter and every local variable (xdebug.collect_params=4 and xdebug.show_local_vars=on configuration parameters).

István Ujj-Mészáros
  • 3,228
  • 1
  • 27
  • 46
2

PHP can't do this out of the box. You'd need to install the xDebug extension on your PHP development machine. Once installed, you could use the code coverage function to determine which lines have executed.

Lacking that, I'd create a simple debug function to include at the top of your code

public function myDebugString($string)
{
    file_put_contents('/tmp/debug.log',"$string\n",FILE_APPEND);
    return;
}

and then add calls to this throughout you code

myDebugString('Called at ' . __LINE__);

And then tail the log file created. Removing the debug statements is a simple find/replace operation for your editor once you're done.

Many frameworks have debugging objects that do way more than this built it, but if you're dealing with stand alone code something simple like this should be enough to get you by.

Alana Storm
  • 164,128
  • 91
  • 395
  • 599
  • Yes it can! debug_backtrace() will do what he wants. Xdebug will give him the possibility of generating a cachegrind file. – AntonioCS Dec 12 '10 at 23:34
  • the debug_backtrace functions are useful, but they only return you a callstack at any given time. It sounds like the OP wants a line-by-line trace of his code, which means a more advanced tool like xdebug. – Alana Storm Dec 13 '10 at 02:54
0

Take a look at code coverage tools. This allows you to identify those functions and lines of code that are actually executed when a script runs

Community
  • 1
  • 1
Mark Baker
  • 209,507
  • 32
  • 346
  • 385