【问题标题】:Logging each error on a new line php在新行 php 上记录每个错误
【发布时间】:2012-01-20 06:40:29
【问题描述】:

我需要在新行上记录每个错误。

file_put_contents('PDOErrors.txt', $e->getMessage() . \n, FILE_APPEND);

这按预期工作,除了所有内容都保留在第一行,所以我尝试使用 \n 似乎不正确。

【问题讨论】:

    标签: php string string-concatenation


    【解决方案1】:

    你应该引用\n

    file_put_contents('PDOErrors.txt', $e->getMessage() . "\n", FILE_APPEND);
    

    或者使用PHP_EOL常量。

    file_put_contents('PDOErrors.txt', $e->getMessage() . PHP_EOL, FILE_APPEND);
    

    【讨论】:

    • @Lake 是对的——尽可能让你的代码跨操作系统兼容。
    • 我使用了EOL(虽然作者在我接受之前删除了解决方案。)
    【解决方案2】:

    另一种方式:

    file_put_contents('PDOErrors.txt', $e->getMessage().PHP_EOL, FILE_APPEND);
    

    【讨论】:

      【解决方案3】:

      您缺少引号。查看更多关于string concatenation in php的信息。

      file_put_contents('PDOErrors.txt', $e->getMessage() ."\n", FILE_APPEND);
      

      或者交叉兼容:

      file_put_contents('PDOErrors.txt', $e->getMessage() .PHP_EOL, FILE_APPEND);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-03-14
        • 1970-01-01
        • 2011-03-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多