W3docs

file_put_contents and a new line help

file_put_contents is a PHP function that writes a string to a file.

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:

Example of file_put_contents function in PHP with a new line to the data being written

php— editable, runs on the server

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

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

Example of using PHP_EOL constant in PHP

php— editable, runs on the server

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.

Example of using the FILE_APPEND flag as the third parameter in file_put_contents function in PHP

php— editable, runs on the server