Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does PHP have a DEBUG symbol that can be used in code?

Tags:

php

debugging

Languages like C and even C# (which technically doesn't have a preprocessor) allow you to write code like:

#DEFINE DEBUG
...

string returnedStr = this.SomeFoo();
#if DEBUG
    Debug.WriteLine("returned string =" + returnedStr);
#endif

This is something I like to use in my code as a form of scaffolding, and I'm wondering if PHP has something like this. I'm sure I can emulate this with variables, but I imagine the fact that PHP is interpreted in most cases will not make it easy to strip/remove the debugging code (since its not needed) automatically when executing it.

like image 997
fuentesjr Avatar asked Oct 10 '08 21:10

fuentesjr


People also ask

Which function is useful for debugging in PHP?

Xdebug. Xdebug is an open-source project that is one of the foremost valuable PHP debug tools. It offers good fundamental debugging support, as well as stack traces, profiling and code scope.

What is debug pack in PHP?

As explained here it's for developers of PHP. I believe it allows you to hook in a debugger for the purpose of hunting down bugs and/or it dumps debugging information when something goes awry. For most bugs, you can just provide a piece of code to reproduce the problem so most bugs do not need that kind of attention.


2 Answers

PHP doesn't have anything like this. but you could definitely whip up something quickly (and perhaps a regex parse to strip it out later if you wanted). i'd do it as such:

define('DEBUG', true);
...
if (DEBUG):
  $debug->writeLine("stuff");
endif;

of course you'd have to write your own debug module to handle all that. if you wanted to make life easier on regex parsing, perhaps you could use a ternary operator instead:

$str = 'string';
DEBUG ? $debug->writeLine("stuff is ".$str) : null;

which would make removing debug lines pretty trivial.

like image 196
Owen Avatar answered Oct 12 '22 02:10

Owen


xdump is one of my personal favorites for debugging.

http://freshmeat.net/projects/xdump/

define(DEBUG, true);

[...]

if(DEBUG) echo xdump::dump($debugOut);
like image 27
Jayrox Avatar answered Oct 12 '22 04:10

Jayrox