When do I use the PHP constant PHP_EOL?
Yes, PHP_EOL
is ostensibly used to find the newline character in a cross-platform-compatible way, so it handles DOS/Unix issues.
Note that PHP_EOL represents the endline character for the current system. For instance, it will not find a Windows endline when executed on a unix-like system.
\n vs. PHP_EOL vs. br?
DOS, Unix, and Mac (pre-OS X and OS X) all use different characters or character combinations to represent "go to the next line."
DOS - Uses a CR+LF (that's ASCII 13 followed by an ASCII 10, or
\r\n
) to represent a new line.Unix - Uses an LF (that's ASCII 10, or
\n
) to represent a new line.Mac (pre-OS X) - Uses a CR (that's ASCII 13, or
\r
) to represent a new line.Mac (OS X) - Like Unix, uses an LF to represent a new line.
Therefore, when to use each one depends on what you're going for. If you're writing for a specific platform without the intention of portability, use the character or character combination to break lines that matter to that platform. The purpose of PHP_EOL
is to automatically choose the correct character for the platform, so that your new lines are platform-independent.
All of these appear as a single space within a browser as browsers collapse whitespace into a display space for display purposes (unless you're using <pre>
as you mentioned, or CSS that changes the behavior of whitespace). This is where <br>
comes in, as you've mentioned, which will convert these \n
new line characters into <br>
so that they provide line breaks in HTML display.
constant like PHP_EOL for tabulation php
Ok I found solution
chr(9)
wrote tabulation to output file
Thanks!
When do I use PHP_EOL instead of \n and vice-versa ? Ajax/Jquery client problem
The constant PHP_EOL
should generally be used for platform-specific output.
- Mostly for file output really.
- Actually the file functions already transform
\n
←→\r\n
on Windows systems unless used infopen(…, "wb")
binary mode.
For file input you should prefer \n
however. While most network protocols (HTTP) are supposed to use \r\n
, that's not guaranteed.
Therefore it's best to break up on
\n
and remove any optional\r
manually:$lines = array_map("rtrim", explode("\n", $content));
Or use the
file(…, FILE_IGNORE_NEW_LINES)
function right away, to leave EOL handling to PHP or auto_detect_line_endings.A more robust and terser alternative is using
preg_split()
and a regexp:$lines = preg_split("/\R/", $content);
The
\R
placeholder detects any combination of \r + \n. So would be safest, and even work for Classic MacOS≤ 9
text files (rarely seen in practice).Obligatory microoptimization note:
While regex has a cost, it's surprisingly often speedier than manual loops and string postprocessing in PHP.
And there are a few classic examples where you should avoid PHP_EOL
due to its platform-ambiguity:
- Manual generation of network protocol payloads, such as HTTP over
fsockopen()
. - For
mail()
and MIME construction (which really, you shouldn't do tediously yourself anyway). - File output, if you want to consistently write just Unix
\n
newlines regardless of environment.
So use a literal "\r\n"
combination when not writing to files, but preparing data for a specific context that expects network linebreaks.
Why PHP_EOL is equal to 0?
What is the good way to remove every PHP_EOL without cutting off 0 ?
$value = rtrim($value, PHP_EOL);
See http://php.net/rtrim.
As for why PHP_EOL
equals 0
, see PHP type juggling, "String" == 0 and "String" == true.
What is the benefit of \n and PHP_EOL in PHP?
A web browser interprets the output of a PHP program as HTML, so \n
and \r\n
will not appear to do anything, just like inserting a newline in an HTML file. On the other hand, <br />
makes a new line in the interpreted HTML (hence "line BReak"). Therefore, <br />
will make new lines, whereas \r\n
will not do anything.
Which is the correct way of using core predefined constant PHP_EOL inside a double quoted string and single quoted string?
echo "$people->john drank some $juices[0] juice.".PHP_EOL;
Here .(dot)
works like it will first concatenate the total string then prints.
But comes to ,
echo "The character at index -2 is $string[-2].", PHP_EOL;
it won't concatenate. it will print one after other based on ,
PHP_EOL seems undefined
Use this:
$text = str_replace(array("\r\n", "\n\r", "\n", "\r"), ' ', $text)
This should cover all the cases regardless of where the system is hosted on.
You can rely on PHP_EOL if all the data is constructed in that OS that is manipulating it, otherwise it is best to use a specific newline character.
Related Topics
How to Make a Calculator in PHP
What Are the "Standard" Timezone Abbreviations
Using Jquery $.Ajax to Call a PHP Function
How to Extract Img Src, Title and Alt from HTML Using PHP
Filter/Remove Rows Where Column Value Is Found More Than Once in a Multidimensional Array
PHP - Should I Call Exit() After Calling Location: Header
PHP MySQLi_Connect: Authentication Method Unknown to the Client [Caching_Sha2_Password]
MySQLi_Query() Expects At Least 2 Parameters, 1 Given
How to Replace "If" Statement With a Ternary Operator ( : )
Detecting Request Type in PHP (Get, Post, Put or Delete)
How to Get a Hex Dump of a String in PHP
What's Better At Freeing Memory With PHP: Unset() or $Var = Null