简体   繁体   English

打印执行的PHP代码(采用的代码路径)

[英]Print the executed PHP code (Path of code taken)

I have a script with alot of nested includes and functions calling each other from lots of if conditions. 我有一个包含大量嵌套include和函数的脚本,它们从很多if条件中相互调用。 Basically, its a coding nightmare. 基本上,这是一场编码梦night。

Is there any way i can "PRINT" the PHP code executed ? 有什么办法可以“打印” PHP代码? I mean, print the actual flow of the code and the path taken by the script from start to end ? 我的意思是,打印代码的实际流程和脚本从头到尾所采用的路径?

You can try debug_backtrace() or debug_print_backtrace() . 您可以尝试debug_backtrace()debug_print_backtrace()

Additionally, I recommend using Xdebug . 另外,我建议使用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). 它在异常上打印出非常有用的堆栈跟踪 (您可以配置它以打印出每个方法参数和每个局部变量( xdebug.collect_params=4xdebug.show_local_vars=on配置参数)。

PHP can't do this out of the box. PHP无法做到这一点。 You'd need to install the xDebug extension on your PHP development machine. 您需要在PHP开发计算机上安装xDebug扩展 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. 许多框架都具有调试对象,它们的功能远不止于构建它,但是,如果您要处理独立代码,那么像这样的简单对象就足够了。

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 这使您可以识别运行脚本时实际执行的那些功能和代码行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM