When do I use the PHP constant “PHP_EOL”?

前端 未结 19 2287
攒了一身酷
攒了一身酷 2020-11-22 06:21

When is it a good idea to use PHP_EOL?

I sometimes see this in code samples of PHP. Does this handle DOS/Mac/Unix endline issues?

19条回答
  •  终归单人心
    2020-11-22 07:19

    No, PHP_EOL does not handle endline issues, because the system where you use that constant is not the same system where you send the output to.

    I would not recommend using PHP_EOL at all. Unix/Linux use \n, MacOS / OS X changed from \r to \n too and on Windows many applications (especially browsers) can display it correctly too. On Windows, it is also easy change existing client-side code to use \n only and still maintain backward-compatibility: Just change the delimiter for line trimming from \r\n to \n and wrap it in a trim() like function.

提交回复
热议问题