Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/245.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
换行符在php中不起作用_Php_File_Text_Newline_File Handling - Fatal编程技术网

换行符在php中不起作用

换行符在php中不起作用,php,file,text,newline,file-handling,Php,File,Text,Newline,File Handling,这是代码。我不知道我在哪里犯了错误。我也尝试了单引号。 我试过了,但所有的东西都排在一行 $output_line=$item."\n"; fwrite($myfile,$output_line); 使用PHP\u EOL而不是\n $output_line=$item . PHP_EOL ; 使用产生\r\n或\n $data = 'some data' . PHP_EOL . 'this should be on new line'; $fp = fopen('my_file',

这是代码。我不知道我在哪里犯了错误。我也尝试了单引号。 我试过了,但所有的东西都排在一行

 $output_line=$item."\n";
 fwrite($myfile,$output_line);

使用
PHP\u EOL
而不是
\n

 $output_line=$item . PHP_EOL ;
使用产生
\r\n
\n

$data = 'some data' . PHP_EOL . 'this should be on new line';
$fp = fopen('my_file', 'a');
fwrite($fp, $data);
//文件输出

some data
this should be on new line

换行符表示形式在不同的操作系统上可能不同。在PHP中,有一个名为PHP_EOL的预定义常量,它为当前平台返回正确的“行尾”符号。从PHP5.0.2开始提供

因此,您的代码应如下所示:

$output_line=$item.PHP_EOL;
fwrite($myfile,$output_line);

您需要使用“内爆”在每个项目之间添加“文本”。
内爆接受一个数组,并在每个项之间添加一个字符串,使其全部为字符串

$output_line=implode(" PHP_EOL", $item);
fwrite($myfile,$output_line);
这将使:

Item1 PHP_EOL
Item2 PHP_EOL
Item3 PHP_EOL

对于一个有效的txt文件,如果它是一个浏览器,它应该是HTML。您可能需要“\r\n”,具体取决于OSU。使用PHP\u EOL而不是“\n”作为测试如何?这样,它应该是系统本身使用的任何返回?有多少项?什么是$item?是字符串中的一个项目还是数组中的几个项目?Andreas,共有7个项目array@Tayyab$item是数组吗?你确定吗?您在下面的评论中已经说过,$item是字符串的答案是正确的。如果$item是一个数组,那么这些答案就不起作用了。你可以在这里读到更多:嗯,这就是我需要的答案。OP说$item是一个数组,这应该会创建一个数组到字符串的转换错误是的,这是事实。但是OP说答案是有效的。OP说$item是一个数组,这应该会产生一个数组到字符串的转换错误