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条回答
smile是对你的礼貌
2楼-- · 2020-02-08 18:00

Make sure that errors are displayed in your development environment (not production).

查看更多
太酷不给撩
3楼-- · 2020-02-08 18:06

Add this to the top of the file:

function shutdown()
{
    print_r(debug_backtrace());
}


register_shutdown_function('shutdown');

See register_ shutdown_function()

查看更多
我想做一个坏孩纸
4楼-- · 2020-02-08 18:11
grep -n die filename
查看更多
手持菜刀,她持情操
5楼-- · 2020-02-08 18:12

Also check the error___logs for "memory_limit" errors in the Apache error_log.

Memory Limit >= 10M Warning: (Set this to 10M or larger in your php.ini file)

In my experience, scripts suddenly end without warning or notice when this happens.

查看更多
爷、活的狠高调
6楼-- · 2020-02-08 18:15

Don't forget to grep for "exit" too.

查看更多
乱世女痞
7楼-- · 2020-02-08 18:16

I use the following code and need no special debugging environment. Note that this might take really long; you can set the ticks count higher - that makes it faster, but blurry.

function shutdown_find_exit()
{
    var_dump($GLOBALS['dbg_stack']);
}
register_shutdown_function('shutdown_find_exit');
function write_dbg_stack()
{
    $GLOBALS['dbg_stack'] = debug_backtrace();
}
register_tick_function('write_dbg_stack');
declare(ticks=1);
查看更多
登录 后发表回答