Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/299.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 Io_Webforms_File Writing - Fatal编程技术网

PHP,写入文件时换行

PHP,写入文件时换行,php,file-io,webforms,file-writing,Php,File Io,Webforms,File Writing,我试图记录我所写表单的所有响应的总数,但是我很难做到每个响应都换一行。我的代码在下面。我只是想让它更容易阅读,因为现在发生的事情是,所有的反应都挤在一起,我想让每一个在一个新的行。我尝试了一些东西,并在代码中对它们进行了注释,结果是什么。提前谢谢 <?php if (isset($_POST['sometext'])) { $myFile = "testFile.txt"; $thetext=$_POST['sometext'] ;//added + "\n" he

我试图记录我所写表单的所有响应的总数,但是我很难做到每个响应都换一行。我的代码在下面。我只是想让它更容易阅读,因为现在发生的事情是,所有的反应都挤在一起,我想让每一个在一个新的行。我尝试了一些东西,并在代码中对它们进行了注释,结果是什么。提前谢谢

<?php
if (isset($_POST['sometext']))
    {
    $myFile = "testFile.txt";
    $thetext=$_POST['sometext'] ;//added + "\n" here but all response turned to 0 
    writemyfile($myFile,$thetext,"a");
    } else
    {
    $thetext="Enter text here";
    }

function readmyfile($thefile)
    {  
        $file = fopen($thefile, "r") or exit("Unable to open file!");
        //Output a line of the file until the end is reached
        while(!feof($file))
        {
            echo fgets($file). "<br />";
        }
        fclose($file);
    }

function writemyfile($thefilename,$data,$mode) 
    {
        $myfile=fopen($thefilename,$mode);
        fwrite($myfile, $data); // added + "\n" here and responses turned 0
        fclose($myfile);
    }  
?>
<html>
    <head>
        <title> Zain's Test Site</title></head>
    <body>
        <form method="post" action="<?php echo $php_self ?>">
            <input type="text" name="sometext" value="<?php echo $thetext ?>" >
            <input type="submit" name="Submit" value="Click this button">
        </form>
        <?php readmyfile("testFile.txt"); ?>
    </body>

扎因试验场
在php中,使用“.”连接字符串,在javascript中使用“+”


在php中,使用“.”连接字符串,在javascript中使用“+”。

能否尝试将换行符(\n)添加到$thetext变量,如下所示:

$thetext=$_POST['sometext'] . "\n";

请记住使用“.”作为连接运算符,并在换行符周围使用双引号。

能否尝试将换行符(\n)附加到$thetext变量,如下所示:

$thetext=$_POST['sometext'] . "\n";
请记住使用“.”作为连接运算符,并在换行符周围使用双引号。

使用换行“\n”而不是用于html的br

使用换行“\n”而不是用于html的br

Err这里有更多的文本来填写答案。

$text=$text.\n“

 fwrite($myfile, $data); // added + "\n" here and responses turned 0
呃,这里有更多的文字来填写答案

 fwrite($myfile, $data); // added + "\n" here and responses turned 0
concat字符串运算符是(.)而不是(+)

你也可以简化你的脚本

 echo nl2br(get_file_contents($file));
concat字符串运算符是(.)而不是(+)

你也可以简化你的脚本

 echo nl2br(get_file_contents($file));

PHP有一个常量,
PHP_EOL
,其中包含主机平台的行尾字符。除非你是专门为另一个平台读/写文件,否则使用它比
\n
更安全。感谢你告诉我这一点,我将对itPHP进行一些研究。PHP有一个常量,
PHP\u EOL
,其中包含主机平台的行尾字符。使用此工具比使用
\n
更安全,除非您专门为另一个平台读/写文件。感谢您告诉我这一点,我将对iThanks进行一些研究。我知道为什么我没有意识到接线员。对不起,浪费了你的时间。谢谢。我知道为什么我没有意识到接线员。对不起,浪费了你的时间。谢谢。我知道为什么我没有意识到接线员。对不起,浪费了你的时间。谢谢。我知道为什么我没有意识到接线员。对不起,浪费了你的时间。谢谢。我知道为什么我没有意识到接线员。对不起,浪费了你的时间。谢谢。我知道为什么我没有意识到接线员。对不起,浪费了你的时间。谢谢。我知道为什么我没有意识到接线员。对不起,浪费了你的时间。这根本不是浪费。这就是我们来这里的原因。:)谢谢我知道为什么我没有意识到接线员。对不起,浪费了你的时间。这根本不是浪费。这就是我们来这里的原因。:)