PHP difference between \r\n and \n

后端 未结 3 1176
一向
一向 2020-12-09 10:25

Simple question...

I have seen people tell me to use \"\\r\\n\" in various places and others tell me to use \"\\n\" in the same place. I\'m sure one is right and one

相关标签:
3条回答
  • \r\n are end of line characters for Windows systems.

    \n is the end of line character for UNIX systems.

    These characters are invisible. From my own experience \n is usually okay for Windows as well.

    Some prefer to use PHP_EOL constant instead of these characters for portability between platforms.

    echo 'hi' . PHP_EOL;
    echo "hi\n";
    
    $headers = "From: webmaster@example.com" . PHP_EOL 
               . "Reply-To: webmaster@example.com"; 
    
    0 讨论(0)
  • 2020-12-09 10:56

    In addition to Yada's answer, here is an explaining blog entry: https://blog.codinghorror.com/the-great-newline-schism/

    [Update 2017-05-24: Fixed the link]

    0 讨论(0)
  • 2020-12-09 10:57

    As per RFC 821 (the SMTP protocol), line endings should always be \r\n (<CR><LF>) in mail headers and content, but in practice it shouldn't matter as most mail servers handle all three type of line endings correctly (supposedly, some old UNIX-based ones actually choke on \r\n but not \n).

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