When do I use the PHP constant “PHP_EOL”?

前端 未结 19 2322
攒了一身酷
攒了一身酷 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 06:58

    On some system may be useful to use this constant because if, for example, you are sending an email, you can use PHP_EOL to have a cross-system script working on more systems... but even if it's useful sometime you can find this constant undefined, modern hosting with latest php engine do not have this problem but I think that a good thing is write a bit code that saves this situation:

    
    

    So you can use PHP_EOL without problems... obvious that PHP_EOL should be used on script that should work on more systems at once otherwise you can use \n or \r or \r\n...

    Note: PHP_EOL can be

    1) on Unix    LN    == \n
    2) on Mac     CR    == \r
    3) on Windows CR+LN == \r\n
    

    Hope this answer help.

提交回复
热议问题