When do I use the PHP constant “PHP_EOL”?

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

    I just experienced this issue when outputting to a Windows client. Sure, PHP_EOL is for server side, but most content output from php is for windows clients. So I have to place my findings here for the next person.

    A) echo 'My Text' . PHP_EOL; // Bad because this just outputs \n and most versions of windows notepad display this on a single line, and most windows accounting software can't import this type of end of line character.

    B) echo 'My Text \r\n'; //Bad because single quoted php strings do not interpret \r\n

    C) echo "My Text \r\n"; // Yay it works! Looks correct in notepad, and works when importing the file to other windows software such as windows accounting and windows manufacturing software.

提交回复
热议问题