开发者

\n not working in my fwrite()

开发者 https://www.devze.com 2022-12-24 17:16 出处:网络
Not sure what could be the problem. I\'m dumping data from an array $theArray into theFile.txt, each array item on a separate line.

Not sure what could be the problem.

I'm dumping data from an array $theArray into theFile.txt, each array item on a separate line.

$file = fopen("theFile.txt", "w");
foreach ($theArray as $arrayItem){
    fwrite($file, $arrayItem . '\n');
}
fclose($file);

Problem is when I open theFile.txt, I see the \n being outputted literally. Also if I try to programmatically read the file line by line (just in case lines are there),开发者_如何学Python it shows them as 1 line meaning \n are really not having their desired effect.


Enclose \n in double quotes as "\n"

Inside a single quote a \n is treated as a literal slash followed by an n, but inside a double quote it is interpreted as a newline char.


Single quotes do not process anything inside the quotes. Any '$' or escaped characters will appear exactly as they are printed with no modification (unless you run them through a function. You will have to use double quotes in order for the '\n' to appear as a line break in the file.

0

精彩评论

暂无评论...
验证码 换一张
取 消