tags:

views:

78

answers:

4

For some reason I can't use \n to create a linefeed when outputting to a file with PHP. It just writes "\n" to the file. I've tried using "\\n" as well, where it just writes "\n" (as expected). But I can't for the life of me figure out why adding \n to my strings isn't creating new lines. I've also tried \r\n but it just appends "\r\n" to the line in the file.

Example:

error_log('test\n', 3, 'error.log');
error_log('test2\n', 3, 'error.log');

Outputs:

test\ntest2\n

Using MAMP on OSX in case that matters (some sort of PHP config thing maybe?).

Any suggestions?

+11  A: 

Use double quotes. "test\n" will work just fine.

If the string is enclosed in double-quotes ("), PHP will interpret more escape sequences for special characters:

http://php.net/manual/en/language.types.string.php

ceejayoz
using single quotes does not change the value into entities so you have to use double! +1
RobertPitt
+1  A: 

It's because you use apostrophes ('). Use quotationmarks (") instead. ' prompts PHP to use whatever is in between the apostrophes literally.

Jan Kuboschek
+1  A: 

You need to use double quotes.

error_log("test\n", 3, 'error.log'); error_log("test2\n", 3, 'error.log');

oshirowanen
+1  A: 

Double quotes are what you want. Single quotes ignore the \ escape. Double quotes will also evaluate variable expressions for you.

Check this page in the php manual for more.

mmsmatt