file_put_contents and a new line help

file_put_contents is a PHP function that writes a string to a file. It can be used to write new data to a file or overwrite existing data. The function takes two required parameters: the file name and the data to be written.

To add a new line to the data being written, you can include a new line character, "n", in the string. For example:

<?php
$data = "This is the first line.\nThis is the second line.";
file_put_contents("example.txt", $data);
$contents = file_get_contents("example.txt");
echo $contents;

This would write "This is the first line." and "This is the second line." to the file "example.txt" on separate lines.

Watch a course Learn object oriented PHP

Alternatively you can use the PHP_EOL constant which automatically uses the correct newline character for the operating system you're running on.

<?php
$data = "This is the first line." . PHP_EOL . "This is the second line.";
file_put_contents("example.txt", $data);
$contents = file_get_contents("example.txt");
echo $contents;

Also, if you want to add data in the next line of the file you can use the FILE_APPEND flag as the third parameter.

<?php
file_put_contents("example.txt", "This is new data", FILE_APPEND);
$contents = file_get_contents("example.txt");
echo $contents;