PHP是否有可以在代码中使用的DEBUG符号? [英] Does PHP have a DEBUG symbol that can be used in code?

查看:98
本文介绍了PHP是否有可以在代码中使用的DEBUG符号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C和C#语言(技术上没有预处理器)可以让您编写如下代码:

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

这是我喜欢在我的代码中用作脚手架形式的东西,我想知道PHP是否有这样的东西。我确信我可以用变量来模拟这个,但是我想象的是,在大多数情况下,PHP被解释并不会在执行时自动删除/删除调试代码(因为它不需要)容易。

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.

推荐答案

PHP没有这样的东西。但是你可以肯定地快速地打开一些东西(如果你想要的话也可能是正则表达式解析)。我会这样做:

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.

这篇关于PHP是否有可以在代码中使用的DEBUG符号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆