dtufl26404 2015-11-14 10:36
浏览 40
已采纳

在这种情况下如何调试PHP代码?

I 'm developing an android app which connects to database using PHP API. It's the first time for me to encounter PHP. What happens is that I "POST" parameters to the php code with a URL to connect to it then php makes a query to my database and store them ... the issue is that all I see is what happens in logcat, I have no idea what is goin on with the PHP so if there is something wrong in there what can I do to debug it ?

Note : I 'm already familiar with echos and var dump I 'm looking for fully debugging tool that will allow me to debug the script without actually running it directly and by that I mean accessing it from my android project.

  • 写回答

1条回答 默认 最新

  • doumao1047 2015-11-14 11:28
    关注

    In such cases i log / append all actions on php side into a file - with a simple

    file_put_contents({file}, {data}, FILE_APPEND);

    . u could also catch nearly every error in php with following methods:

    set_error_handler({YOUR_ERROR_HANDLER}, E_ALL);
    
    register_shutdown_function({YOUR_ERROR_HANDLER});
    

    http://php.net/manual/de/function.set-error-handler.php

    http://php.net/manual/de/function.register-shutdown-function.php

    example code of how would i test an envoirement in php, which i could not debug direct:

    <?php
    
    ini_set('display_errors', 1);
    error_reporting(E_ALL);
    
    // will be triggered on every php shutdown - will log the last error (if one occurs)
    register_shutdown_function(function()
    {
        // get last error
        $aError = error_get_last();
    
        if(!empty($aError))
        {
            logAction('called from register_shutdown_function | ' . $aError['message']);
        }
    });
    
    // will be triggered on every error
    set_error_handler(function($iErrCode, $sErrorText, $sErrorFile = '', $sErrorLine = '')
    {
        logAction('called from set_error_handler | Code: ' . $iErrCode . ' Text: ' . $sErrorText . ' File: ' . $sErrorFile . ' Line: ' . $sErrorLine);
    }, E_ALL);
    
    // will be triggered on every exception
    set_exception_handler(function(\Exception $e)
    {
        logAction('called from set_exception_handler | Code: ' . $e->getCode() . ' Text: ' . $e->getMessage() . ' File: ' . $e->getFile() . ' Line: ' . $e->getLine());
    });
    
    // main log / debug method
    function logAction($sText)
    {
        $sDate = date('Y.m.d H:i:s');
        file_put_contents('debug.log', $sDate . ': ' . $sText, FILE_APPEND);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?