newline in error_log in php

As mentioned before, you can use either PHP_EOL or use double quotes in order to output a newline to your log file.

Anyway, when using the Linux console in order to debug the application, the tail command will show the new line as \\\\n.

A simple solution is to use sed in order to replace \\\\n with \\n:

tail -f file | sed 's/\\n/\n/g'

See this answer: Display \n characters as newlines when using tail?


Use double quotes when adding the error message.

error_log("This is a two lined message. \nThis is line two.");

should work.

error_log('This is a one lined message. \nThis is the same line still.');

will not work: notice the single quotes.


PHP_EOL manages newlines on multiple platforms:

error_log("Error message" . PHP_EOL, 3, 'error.log');

Tags:

Php