When do I use the PHP constant “PHP_EOL”?

前端 未结 19 2556
攒了一身酷
攒了一身酷 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:05

    PHP_EOL (string) The correct 'End Of Line' symbol for this platform. Available since PHP 4.3.10 and PHP 5.0.2

    You can use this constant when you read or write text files on the server's filesystem.

    Line endings do not matter in most cases as most software are capable of handling text files regardless of their origin. You ought to be consistent with your code.

    If line endings matter, explicitly specify the line endings instead of using the constant. For example:

    • HTTP headers must be separated by \r\n
    • CSV files should use \r\n as row separator

提交回复
热议问题