dongxia4880 2012-02-08 21:35
浏览 41
已采纳

在PHP中是否有一个print_r或var_dump等效,默认情况下不会回显结果?

I often use print_r($value, true) to dump some variables in log statements, and sometimes I forget to set the second parameter to true, which then might result in instead of the result being in the log statement, in most cases it just gets lost, but sometimes it is even rendered to the users browser.

This just happened to me, and within the object graph where some credentials, and other things that you normally would not like the end user to see. The problem was that instead of passing true as the second parameter, for some reason I passed null 1 year ago. And now there was some system failure that made the wrong output show.

What do you do to avoid having stacktraces output by programmer error? Why do all PHP dumping functions just echo the output by default? Searching though our codebase, I found quite a lot of invocations of print_r without the second parameter set to true.

I also use json_encode() for debug output sometimes, because the output is sometimes more concise and friendly to read. Any drawbacks with that approach?

We normally use some sort of output buffering, but not everywhere.

  • 写回答

5条回答 默认 最新

  • duansao20000508 2012-02-08 21:40
    关注

    Why not create a custom function that will wrap all your print_r calls? I use something like this:

    function good_print() {
        $log = '';
        foreach(func_get_args() as $arg) $log .= print_r($arg, true);
        return $log;
    }
    

    It saves time, gives me better functionality, plus I don't have to worry about "did I use the right call this time?"

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)