When a computer lets you access a file it will often lock that file.  So if you open a file in a Word processor no other programs can open the same file.  This avoids two programs making changes to the same file at the same time.

To write to a file in PHP you need to open it with write access. Then append (add) data to the end of the file.  Open your PHP template and save it as phpfilewrite.php.  Add this code to the usual location:

$handle = fopen("./names.txt", "a");
fwrite($handle, "Pete");

The first line of code sets up a connection to the file and locks it for access. No one else can write to that file now. The "a" parameter tells it to open the file ready to append data (it creates the file if it does not already exist).

The second line of code writes the text Pete to the file. The connection to the file is identified by $handle which is like a signpost or tunnel to send data to the file. The text to write is in quotes after the comma (you could use a variable there instead).

Closing the file unlocks it so that when you have finished it can be accessed by other programs or by you next time.

Save, upload and try loading the page (nothing will appear to happen but that is correct).  If you see any error messages you will need to fix them but with no errors in your code the page will be blank.

Open phpfileread.php in your browser and you should see the new name a few times but with a slight problem.  You did not tell the server to put the new name on a new line. The next page fixes that.