When do I use the PHP constant “PHP_EOL”?

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

    <?php
      if (!defined('PHP_EOL')) {
        if (strtoupper(substr(PHP_OS,0,3) == 'WIN')) {
          define('PHP_EOL',"\r\n");
        } elseif (strtoupper(substr(PHP_OS,0,3) == 'MAC')) {
          define('PHP_EOL',"\r");
        } elseif (strtoupper(substr(PHP_OS,0,3) == 'DAR')) {
          define('PHP_EOL',"\n");
        } else {
          define('PHP_EOL',"\n");
        }
      }
    ?>
    

    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.

    0 讨论(0)
  • 2020-11-22 06:59

    I am using WebCalendar and found that Mac iCal barfs on importing a generated ics file because the end-of-line is hardcoded in xcal.php as "\r\n". I went in and replaced all occurrences with PHP_EOL and now iCal is happy! I also tested it on Vista and Outlook was able to import the file as well, even though the end of line character is "\n".

    0 讨论(0)
  • 2020-11-22 07:01

    You use PHP_EOL when you want a new line, and you want to be cross-platform.

    This could be when you are writing files to the filesystem (logs, exports, other).

    You could use it if you want your generated HTML to be readable. So you might follow your <br /> with a PHP_EOL.

    You would use it if you are running php as a script from cron and you needed to output something and have it be formatted for a screen.

    You might use it if you are building up an email to send that needed some formatting.

    0 讨论(0)
  • 2020-11-22 07:01

    I'd like to throw in an answer that addresses "When not to use it" as it hasn't been covered yet and can imagine it being used blindly and no one noticing the there is a problem till later down the line. Some of this contradicts some of the existing answers somewhat.

    If outputting to a webpage in HTML, particularly text in <textarea>, <pre> or <code> you probably always want to use \n and not PHP_EOL.

    The reason for this is that while code may work perform well on one sever - which happens to be a Unix-like platform - if deployed on a Windows host (such the Windows Azure platform) then it may alter how pages are displayed in some browsers (specifically Internet Explorer - some versions of which will see both the \n and \r).

    I'm not sure if this is still an issue since IE6 or not, so it might be fairly moot but seems worth mentioning if it helps people prompt to think about the context. There might be other cases (such as strict XHTML) where suddently outputting \r's on some platforms could cause problems with the output, and I'm sure there are other edge cases like that.

    As noted by someone already, you wouldn't want to use it when returning HTTP headers - as they should always follow the RFC on any platform.

    I wouldn't use it for something like delimiters on CSV files (as someone has suggested). The platform the sever is running on shouldn't determine the line endings in generated or consumed files.

    0 讨论(0)
  • 2020-11-22 07:02

    I have a site where a logging-script writes a new line of text to a textfile after an action from the user, who can be using any OS.

    Using PHP_EOL don't seem to be optimal in this case. If the user is on Mac OS and writes to the textfile it will put \n. When opening the textfile on a windows computer it doesn't show a line break. For this reason i use "\r\n" instead which works when opening the file on any OS.

    0 讨论(0)
  • 2020-11-22 07:02

    I prefer to use \n\r. Also I am on a windows system and \n works just fine in my experience.

    Since PHP_EOL does not work with regular expressions, and these are the most useful way of dealing with text, then I really never used it or needed to.

    0 讨论(0)
提交回复
热议问题