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

Languages ​​such as C and even C # (which technically do not have a preprocessor) allow you to write code, for example:

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

This is what I like to use in my code as a form of forests, and I wonder if PHP has something like this. I am sure that I can imitate this using variables, but I believe that PHP interpretation in most cases will not facilitate the removal / removal of debug code (from the moment of its absence) automatically when it is executed.

+4
source share
3 answers

PHP has nothing of the kind. but you could definitely hack something quickly (and possibly parsing to strip it later if you like). I would do it as such:

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

Of course, you have to write your own debugging module to handle all this. if you want to make life easier when parsing regular expressions, perhaps you could use the ternary operator:

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

which will make deleting debug lines pretty trivial.

+7
source

xdump is one of my personal favorites for debugging.

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

 define(DEBUG, true); [...] if(DEBUG) echo xdump::dump($debugOut); 
+1
source

It has define funciton described here: http://us.php.net/manual/en/language.constants.php .

Given the set of differences between the variables and constants described in the documentation, I assume that PHP define allows the interpreter to exclude unused code paths at compile time, but this is just an assumption.

- Douglas Hunter

0
source

Source: https://habr.com/ru/post/1277663/


All Articles