打印执行的 PHP 代码(采用的代码路径)
我有一个包含大量嵌套包含和函数的脚本,这些包含和函数从大量 if 条件中相互调用。基本上,这是一场编码噩梦。
有什么方法可以“打印”执行的 PHP 代码吗?我的意思是,打印代码的实际流程以及脚本从开始到结束所采取的路径?
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 ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
PHP 无法开箱即用地执行此操作。您需要在 PHP 开发计算机上安装xDebug 扩展。安装后,您可以使用代码覆盖率函数来确定哪些行已执行。
如果缺少这一点,我将创建一个简单的调试函数以包含在代码顶部
,然后在整个代码中添加对此的调用
,然后跟踪创建的日志文件。完成后,删除调试语句对于编辑器来说是一个简单的查找/替换操作。
许多框架都有调试对象,其功能远不止于此,但如果您正在处理独立代码,像这样的简单内容应该足以让您通过。
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
and then add calls to this throughout you code
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.
您可以尝试 debug_backtrace() 或 debug_print_backtrace()。
此外,我建议使用Xdebug。它在异常时打印非常有用的堆栈跟踪(您可以将其配置为打印出每个方法参数和每个局部变量(
xdebug.collect_params=4
和xdebug. show_local_vars=on
配置参数)。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
andxdebug.show_local_vars=on
configuration parameters).查看代码覆盖率工具。这使您可以识别脚本运行时实际执行的那些函数和代码行
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