'Creating new lines when writing to a .log file from a php script
I am writing to a .log file called mylog.log from a php script in the following way
file_put_contents('mylog.log', "/ntestline");
This part of the script is run a number of times. It seems to be writing every entry on the same line and when I run it for two long the log file simply reads.
ASCII text, with very long lines
How do I add new lines when writing to a .log file from a php script?
Solution 1:[1]
As David Ericsson posted in his commented, you should use the constant PHP_EOL as this will insert the correct end of line sequence for the server operating system.
file_put_contents('mylog.log', PHP_EOL . "testline", FILE_APPEND);
That said, most text viewers/editors can handle either \n or \r\n - and the more intelligent ones will continue using the same sequence if you further edit the file.
Since PHP is most at home on Linux, I'd recommend using the \n if you're not using PHP_EOL
Solution 2:[2]
To add newlines in plaintext (like a log), use "\n" in your string. example:
$string = "some text \n some more text that will be on a new line";
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Jay Are |
