Fastest way to determine where PHP script exits

2020-02-08 17:35发布

Say you have a large PHP project and suddenly, when attempting to run it, you just end up with a blank page. The script terminates and you want to find exactly where that is with as little effort as possible.

Is there a tool/program/command/IDE that can, on PHP script termination, tell you the location of a script exit?

Note: I can't mark my own post as "accepted answer" so look at the bottom to see my solution. If you come up with a better solution I will mark your post as the answer.

标签: php
9条回答
够拽才男人
2楼-- · 2020-02-08 18:18

With some inspiration from the nonworking but still right-direction answer from RoBorg, I used the following code in the beginning:

function shutdown() {
    global $dbg_stack_a;
    print_r($dbg_stack_a);
}
register_shutdown_function('shutdown');

And then I made a global conditional breakpoint (global = breakpoint is evaluated on each row), exploiting the fact that it can run code trough eval(), with the following "condition":

eval('
global $dbg_stack_a, $dbg_stack_b, $dbg_stack_c;
$dbg_stack_a = $dbg_stack_b;
$dbg_stack_b = $dbg_stack_c;
$dbg_stack_c = debug_backtrace();
return false;
')

Probably not fast but does the trick! Using this I was able to determine the exact file and line location that raised die(). (This example works in NuSphere.)

查看更多
甜甜的少女心
3楼-- · 2020-02-08 18:21

You can use an interactive debugger to step through the code until you reach the exit point. Other than that, I think you're down to grep'ing the code for exit|die.

查看更多
够拽才男人
4楼-- · 2020-02-08 18:21

xdebug has a nice trace feature that'll allow you to see all the entire trace of your php app execution and it should give you give clue as to where your exit is.

but for the quick and dirty solution a grep/find as mentioned above will do rightly.

查看更多
登录 后发表回答