Home>Article>Backend Development> What does PHP_EOL mean?
PHP_EOL is a defined variable, representing the newline character of PHP. This variable will change according to the platform. It will be "/r/n" under Windows and "/n" under Linux. Under mac it is "/r". Generally, you can use "str_replace(PHP_EOL,'',string)" to remove newlines, which is compatible with various platforms.
The operating environment of this tutorial: windows7 system, PHP8 version, DELL G3 computer
PHP_EOL is a defined variable. Represents the newline characterin PHP. This variable will change according to the platform. It will be /r/n under Windows, /n under Linux, and /r under Mac.
To remove newlines, just press the following:
$str = str_replace(PHP_EOL, '', $str);
PHP_EOL is ostensibly used to find newlines in a cross-platform compatible way, so it handles DOS/Unix issues.
Please note that PHP_EOL represents the end character of the current system. For example, when executed on a unix-like system, it will not find the Windows endline.
PHP_EOL You can use when you want a new row, and you want it to be cross-platform.
This may be when you write a file to the file system (log, export, other).
You can use this if you want the generated HTML to be readable. So, you can follow your
with a PHP_EOL.
You would use this if you were running php from cron as a script and you needed to output something and format it for the screen.
You can use this if you want to structure an email to send, which requires some formatting.
I find PHP_EOL very useful for file processing, especially if you are writing multiple lines to a file.
For example, you have a long string that you want to split into multiple lines when written to a normal file. Using \r\n may not work, so just put PHP_EOL in the script and the result will be great.
php Line break description
A small line break actually has different implementations on different platforms. Why is this? The world is diverse. Originally, /n was used to replace line breaks in the Unix world, but in order to reflect the difference, Windows uses /r/n. What is more interesting is that /r is used in Mac. Therefore, the Unix series uses /n, the Windows series uses /r/n, and the Mac uses /r. This will cause a lot of trouble for the program you write to run on different platforms. Here are some common ways to remove newlines in PHP.
The first way of writing:
$content=str_replace("\n","",$content); echo $content;
The second way of writing:
str_replace("\r\n","",$str);
The third way of writing:
$content=preg_replace("/\s/","",$content); echo $content;
About\n,\ r,\t
\n Soft return: In Windows, it means a line break and returns to the beginning of the next line. In Linux and Unix, it only means a line break, but Does not go back to the beginning of the next line.
\r Soft space: In Linux and Unix, it means returning to the beginning of the current line. In Mac OS, it means breaking a line and returning to the beginning of the next line, which is equivalent to the effect of \n in Windows.
\t Tab (move to the next column)
A few notes:
They are enclosed in double quotes or fixed It is valid in strings represented by delimiters and invalid in strings represented by single quotes.
\r\n is generally used together to represent the Enter key on the keyboard (in Linux and Unix), or only \n (in Windwos). In Mac OS, \r is used to represent Enter.
\tRepresents the "TAB" key on the keyboard.
Line breaks in files: windows: \n, linux, unix: \r\n
Supplementary code:
Recommended learning: "PHP Video Tutorial》
The above is the detailed content of What does PHP_EOL mean?. For more information, please follow other related articles on the PHP Chinese website!