Re: PHP EOL

From: Date: Sat, 02 Jul 2011 14:13:32 +0000
Subject: Re: PHP EOL
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-313839@lists.php.net to get a copy of this message
On Jul 2, 2011, at 3:01 AM, Karl DeSaulniers wrote:
Hello All, Happy pre independence for my American PHPers. And good health to all others. Have a quick question.. I have this code I use for the end of line characters used in my mailers. [Code] // Is the OS Windows or Mac or Linux if (strtoupper(substr(PHP_OS,0,5)=='WIN')) { $eol="\r\n"; } else if (strtoupper(substr(PHP_OS,0,5)=='MAC')) { $eol="\r"; } else { $eol="\n"; } [End Code] Does this suffice or should I be using the php supplied end of line? $eol=PHP_EOL; Or do these do the same thing? What advantages over the code I use does the PHP_EOL have? Or does it not matter with these and either are good to go? It seems to me that they do the same thing.. am I on the right track or missing something? Is there any other OS's that are not WIN or MAC and use the "\r" or "\r\n" ? If their are, then I can see an advantage of using the PHP_EOL. Like I said, just a quick question. ;) Karl DeSaulniers Design Drumm http://designdrumm.com
What's interesting is that the SMTP standard actually requires a CRLF, even in unix-land, so PHP_EOL may not work as intended if you're using it there. From RFC-2821:
        2.3.7 Lines
        SMTP commands and, unless altered by a service
        extension, message data, are transmitted in "lines". Lines consist
        of zero or more data characters terminated by the sequence ASCII
        character "CR" (hex value 0D) followed immediately by ASCII
        character "LF" (hex value 0A). This termination sequence is denoted
        as <CRLF> in this document. Conforming implementations MUST NOT
        recognize or generate any other character or character sequence
        as a line terminator. Limits MAY be imposed on line lengths by
        servers (see section 4.5.3).
        In addition, the appearance of
        "bare" "CR" or "LF" characters in text (i.e., either without the
        other) has a long history of causing problems in mail implementations
        and applications that use the mail system as a tool. SMTP client
        implementations MUST NOT transmit these characters except when
        they are intended as line terminators and then MUST, as indicated
        above, transmit them only as a <CRLF> sequence.
The old sendmail operated this way. I believe more modern MTAs handle either CRLF, CR, or LF terminated lines on input, so in practice it may not matter. Also, as of OSX, Mac uses LF as the line terminator character, not CR, although I think it will handle either as a backwards compatibility feature. Elsewhere, such as in browser output, PHP_EOL is the best bet to cause a line termination outside of mail applications. In actual practice, however, PHP_EOL may be less useful than it would appear, as it only provides the appropriate line terminator based on the host it is running on. If your server is running a flavour of unix, and your running on windows, for example, the server will output '\n' to your browser, not '\r\n' as Windows would like. I believe all the modern browsers, regardless of platform OS, handle all three CRLF, CR, and LF and treat them as line terminators. However if you dump the return from the server to a file, you won't see this behaviour, obviously. There's a pretty long thread on this at stackoverflow.com: http://stackoverflow.com/questions/128560/when-do-i-use-the-php-constant-php-eol I tend to use PHP_EOL inplace of specifying a "\n" character string because to me it is clearer and more obvious what I mean, and does make the code at least a bit more portable.

« previous php.general (#313839) next »